I am using a variable to reference the name of a sheet in my workbook. It works in one place, but not in another. I know (KNOW!) that I am missing something simple, but I'm not seeing it. Below are the appropriate lines, copied exactly from my VBA in case there are typos (but the error doesn't happen in compile, only when running).
SETUP SCRIPT:
Dim lastrow As Integer
Dim sourcesheet As String
Dim Var1 As Integer
Dim Var2 As Integer
sourcesheet = "Final"
Var1 = 2
Var2 = 2
WORKING SCRIPT:
lastrow = Worksheets(sourcesheet).Range("B1").End(xlDown).Row
NOT WORKING SCRIPT:
Debug.Print Worksheets(sourcesheet).Cells(Var1, Var2)
What am I missing? I get "Run-time error '1004': Application-defined or object-defined error" when I try to run the debug.print line. I've checked that Var1 and Var2 are holding the right values, that the cell isn't blank (just in case).
I tried the following, but none of these works, either:
Debug.Print Worksheets(sourcesheet).Cells(Var1, Var2).Value
Debug.Print ThisWorkbook.Worksheets(sourcesheet).Cells(Var1, Var2)
Debug.Print ThisWorkbook.Worksheets(sourcesheet).Cells(Var1, Var2).Value
Thanks for any help you can give me.
SETUP SCRIPT:
Dim lastrow As Integer
Dim sourcesheet As String
Dim Var1 As Integer
Dim Var2 As Integer
sourcesheet = "Final"
Var1 = 2
Var2 = 2
WORKING SCRIPT:
lastrow = Worksheets(sourcesheet).Range("B1").End(xlDown).Row
NOT WORKING SCRIPT:
Debug.Print Worksheets(sourcesheet).Cells(Var1, Var2)
What am I missing? I get "Run-time error '1004': Application-defined or object-defined error" when I try to run the debug.print line. I've checked that Var1 and Var2 are holding the right values, that the cell isn't blank (just in case).
I tried the following, but none of these works, either:
Debug.Print Worksheets(sourcesheet).Cells(Var1, Var2).Value
Debug.Print ThisWorkbook.Worksheets(sourcesheet).Cells(Var1, Var2)
Debug.Print ThisWorkbook.Worksheets(sourcesheet).Cells(Var1, Var2).Value
Thanks for any help you can give me.
Last edited: