VB.NET: How can I copy an Excel worksheet?

Posted on 2008-11-09
Last Modified: 2013-11-07
Hello all,

I'm trying to use VB.NET to automate some tedious manual Excel data entry we have going on. We have an Excel file where we keep track of some tests we're running. For every test, we add a new tab/worksheet to the Excel file.

Ideally, I'd like to use VB.NET to open the excel file, automatically make a copy of the "current" tab/worksheet, and replace the data with my new test results, so all the formatting and everything stays the same.

I know how to use VB.NET to edit cells in an excel worksheet, but am having trouble copying a specific sheet.

Any comments/suggestions would definitely be welcome, thanks!

Question by:Inward_Spiral
    LVL 12

    Accepted Solution

    Hello, Inward Spiral,

    The code in the attached snippet works for me (i.e. copies the WorkSheet named "Sample" in the designated Workbook).

    Perhaps if you show the code you are trying to use, someone can pinpoint the problem.
            Dim xlaTest As Excel.Application = CType(CreateObject("Excel.Application"), Excel.Application)
            xlaTest.Visible = True
            Dim wbkTest As Excel.Workbook = xlaTest.Workbooks.Open("D:\Tests\VB Trials\ExcelTests\TestWorkbook.xls")
            Dim wshTest As Excel.Worksheet = DirectCast(wbkTest.Worksheets("Sample"), Excel.Worksheet)

    Open in new window


    Author Comment

    That did it, thanks!

    Expert Comment

    What if I want to copy an Excel sheet and then only modify select cells and rename it based upon input to a textbox in a form?  I'm getting errors when debugging on renaming the worksheet.  When I turn that off, then I get errors on the "oSheet2.Cells.Range("D123") = TextBox1.Text".... saying that "Public member 'Cells' on type 'Boolean' not found."  I also get the message if I have "oSheet2.range("D123") = TextBox1.Text".  Any suggestions????  I'd appreciate some help on this.  Some of the text boxes hold file paths, others hold numeric values, and some are mixed text and numbers.  I'm not sure if there needs to be differences in how each is set up or not.

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim MyWBName As Object
            Dim oExcel As Object
            Dim oBook As Object
            Dim oSheet As Object
            Dim oSheet2 As Object
            oExcel = CreateObject("Excel.Application")
            oExcel.Visible = True
            MyWBName = TextBox26.Text
            'MyWBName = oExcel.GetOpenFilename()
            oBook = oExcel.Workbooks.Open(MyWBName)
            ' MsgBox("The current workbook is " & MyWBName)
            oSheet = oExcel.ActiveSheet
            'oSheet.Copy(After:=oSheet)  'copies worksheet, creating new worksheet with Scenario Name
            'oSheet2 = oSheet.Copy(After:=oBook.Sheets(3))
            ' MsgBox("The current sheet is " & oSheet)
            oSheet = oSheet.Copy(after:=oSheet)
            oSheet2 = oSheet
            'oSheet2.Name = ScenarioName   'Renames the new worksheet oSheet2
            'File Locations
            oSheet2.Cells.Range("D248").Value = TextBox9.Text                    'Snapshot File
            oSheet2.Cells.Range("D155").Value = TextBox10.Text                  'Currents File
            oSheet2.Cells.Range("D119").Value = TextBox17.Text                  'Met Data File
            oSheet2.Cells.Range("D557").Value = TextBox20.Text                  'Shoreline File
            oExcel = Nothing
        End Sub

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now