简体   繁体   中英

Open and update an existing workbook with 2 worksheets

This may be simplistic to most of you. I just started using VBA to create and update excel workbooks. I found some code on the internet to open and update an existing workbook and worksheet. Like I said, I am brand new at this. Does this code even make sense? I just need to know how to open an existing workbook and all the examples I have found aren't working in our environment.Thanks for any help I can get

Dim wbSource, xlApp, srcWorksheet 

 'initialize
 Set xlApp = CreateObject("Excel.Application")

 'open source and target files
 Set wbSource = lApp.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
 set srcWorksheet = wbSource.Worksheets("NewCycle")
 srcWorksheet.sheets("NewCycle").Activate

 srcWorksheet.Rows("1:1").Delete 

If you are in Excel VBA, this isn't quite what you want. This code was written for an external app (say written in VB6) to open Excel remotely and then do stuff to that copy of Excel. If you are already in Excel/VBA you obviously don't need to do that.

In VBA, the equivalent code would be something like this:

Public Sub MyCode()
    Dim wb as Workbook
    Dim ws as Worksheet 

    Set wb = Application.Workbooks.Open("X:\GCIXCycleCompare_test_auto.xlsx")
    Set ws = wb.Worksheets("NewCycle")

    ws.Rows(1).Delete
End Sub

If you run this code (by click F5 from inside VBA ... or by run macro in Excel) it should open up the test file off the X: drive, and then delete the first row of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM