3

What is the proper syntax for referencing a different workbook's worksheet? The following code is throwing an error on the last line. Thanks!

'Instantiate Workbook variables
 Dim mWB As Workbook 'master workbook

'Instantiate Worksheet variables
 Dim mWS As Worksheet 'master worksheet

'Open Workbook, set variables and reference range
 Workbooks.Open ("Local 500GB HD:Users:user:Public:file.xlsx")
 Set mWB = ActiveWorkbook
 Set mWS = Sheets("Invoices")
 mWB.mWS.Range("A1").FormulaR1C1 = "Import Date" ' <---- This is the where the error is
4

1 回答 1

7

改变

Set mWS = Sheets("Invoices")

Set mWS = mWb.Sheets("Invoices")

然后写mWS.Range("A1").FormulaR1C1 = "Import Date"在最后一行。

实际上,您可以像我上面放置的那样将最后一行更改为读取,因为您ActiveWorkbook没有更改,但是,最好的做法是准确限定所有变量,因此不会发生意外行为。

于 2012-10-30T19:47:25.500 回答