On setting Ranges in VBA
This post is probably not going to appeal to many of you, but it’s my blog and I shall do what I want with it. And what I want right now is some help with programming in VBA. Someone out there must know the answer…
So, I have a spreadsheet which looks like this:
I would like to write code which selects the data set, like this:
But as I don’t always know the size of the data set, I cannot use this code:
Range("A4:F21").Select
I have therefore attempted to write something like this:
With Range(“A4”)
.Range(.Offset(0, 0), .End(xlDown).End _(xlToRight)).Name = "DataSet"
End With
Range(“DataSet”).Select
Unfortunately, even though .Offset(0, 0).Address is A4, and .End(xlDown).End(xlToRight).Address is F21 my code actually selects this:
Why?
(Incidentally, the following alternative does not compile):
With Range(“A4”)
.Range(“.Offset(0, 0) : .End(xlDown).End _(xlToRight)”).Name = "DataSet"
End With
Range(“DataSet”).Select
Comments
The answer, it seems, lies in the fact that I should have used Range(.Offset ... rather than .Range(.Offset ...
As ever, it's all in the punctuation.