There is nothing wrong about using CurrentRegion
with regards to A1
as long as you are aware of what it returns.
It returns the contiguous range as defined by row 1 and column 1.
The range ends horizontally at the column immediately to the left of the first truly empty cell in row 1. A zero-length string as the result of a formula is NOT a true blank.
The range ends vertically at the row immediately above the first truly empty cell in column A.
If A1 is empty the CurrentRegion of A1 is A1.
If the above suits your purposes then you do not need the variables in your example at all. You can get the address from A1 to the last row and column of the A1's current region like so:
MsgBox ThisWorkbook.Sheets("Sheet1").[a1].CurrentRegion.Address
.
If you want an object variable then do this:
Set r = ThisWorkbook.Sheets("Sheet1").[a1].CurrentRegion
Now that the entire range is represented by r
, you could select it like this:
r.Parent.Activate
r.Select
Note: if using CurrentRegion
on any other range besides [A1] the definition of what it returns is more complex because it looks for contiguous data both above and to the left; [A1] has no such areas.
Note: if you need to find the last row and column allowing for truly empty cells in row 1 and/or column A then CurrentRegion
will not achieve the desired result. You'll need to use Find.