most
Board Regular
- Joined
- Feb 22, 2011
- Messages
- 106
- Office Version
- 365
- 2019
- Platform
- Windows
- Mobile
Why does this code work when the sheet is selected and not when another sheet is selected/active?
I get "Run-time error '1004': Application-defined or object-defined error."
This also works in the same way...
For Each r In Sheets("Data1").Range(Cells(LastRow_D1, 1), Cells(LastRow_D1, LastColumn_D1))
VBA Code:
Dim r As Range
Dim LastRow_D1 As Integer
Dim LastColumn_D1 As Integer
LastRow_D1 = LastRowColumn(Sheets("Data1"), "R")
LastColumn_D1 = LastRowColumn(Sheets("Data1"), "C")
For Each r In Sheets("Data1").Range(Cells(LastRow_D1, 1), Sheets("Data1").Cells(LastRow_D1, LastColumn_D1))
Debug.Print r.Value
Next
This also works in the same way...
For Each r In Sheets("Data1").Range(Cells(LastRow_D1, 1), Cells(LastRow_D1, LastColumn_D1))