1

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?

Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
Hana
  • 81
  • 6
  • [Here](https://excelmacromastery.com/excel-vba-worksheet/#Subscript_Out_of_Range) you find the typical situations which lead to run-time-error 9 – Storax Dec 30 '18 at 10:15
  • when you say the workbook isn't active, it's vague.. Is the workbook open? If it is/isn't determines how to solve your problem – alowflyingpig Dec 30 '18 at 22:25

3 Answers3

0

This syntax is correct:

Set trackSheet = trackBook.Worksheets("sheet1")

and the subscript out of range error suggests the workbook in question doesn't contain a worksheet named "sheet1".

This syntax is incorrect:

firstEmpty = trackBook.Worksheet("sheet1").Range...

and the error message indicates that the "trackBook" object doesn't contain a property or method Worksheet (should be plural: Worksheets).

I would run under the debugger, break on the line Set trackSheet = trackBook.Worksheets("sheet1"), and examine the Worksheets property in the Watch window. In particular, examine the names of each worksheet.

Joe
  • 122,218
  • 32
  • 205
  • 338
0

Work with Workbooks.Open method (Excel)

Example

Option Explicit
Public Sub Example()
    Dim xlBook As Excel.Workbook
    Set xlBook = Workbooks.Open("C:\Temp\Book1.xlsm")

    Debug.Print xlBook.Sheets("Sheet1").Range("A1").Value

        xlBook.Close SaveChanges:=False
End Sub
0m3r
  • 12,286
  • 15
  • 35
  • 71
0

Try the code below, it's a little longer, but it has error handling for Workbook and Worksheet scenarios.

Dim trackBook As Workbook
Dim trackSheet As Worksheet
Dim lastRow As Long, firstEmpty As Long


' set the workbook object (if workbook is already open)
On Error Resume Next
Set trackBook = Workbooks("Tracking Sheet.xlsx")
On Error GoTo 0
If trackBook Is Nothing Then ' workbook is not open >> open it
    trackBook = Workbooks.Open(Filename:="C:\YourEntirePath\Tracking Sheet.xlsx")
End If

' set the worksheet object
On Error Resume Next
Set trackSheet = trackBook.Worksheets("sheet1")
On Error GoTo 0
If trackSheet Is Nothing Then
    MsgBox "Worksheet 'sheet1' doesn't exists in workbook, check sheet's name", vbCritical, "Worksheet Name Error"
    Exit Sub
End If

With trackSheet
    firstEmpty = .Cells(.Rows.Count, "A").End(xlUp).Row + 1 ' get first empty row in column A
End With
Shai Rado
  • 33,032
  • 6
  • 29
  • 51
  • Thanks, I actually have error checking before the code I brought here, I didn't put it because it wasn't relevant. – Hana Dec 31 '18 at 06:40