I can't figure it out what is the right way to reference a sheet on a workbook that is not active.
I have the following code:
Dim lastRow As Integer
Dim firstEmpty As Integer
Dim trackBook As Workbook
Dim trackSheet As Worksheet
Set trackBook = Application.Workbooks.Item("Tracking Sheet.xlsx")
lastRow = Range("A" & Rows.Count).End(xlUp).Row
And now I want to refer to a sheet on trackBook. I tried to set a variable:
Set trackSheet = trackBook.Worksheets("sheet1")
And I get run-time error 9: subscript out of range.
I also tried to do it without setting a worksheet variable:
firstEmpty = trackBook.Worksheet("sheet1").Range("A" & Rows.Count).End(xlUp).Row + 1
and I get run-time error 438: Object doesn't support this property or method.
What am I doing wrong in both cases and I should this be done?