557 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Relative Selection using Excel VBA Macros
Part four of a six-part series of blogs
This blog shows the various techniques within Excel Visual Basic for moving around and selecting cells relative to the active cell or selection. The blog includes a discussion of the Offset, End, Resize, Range, EntireRow, EntireColumn and CurrentRegion methods.
A common requirement is to select the block of cells from the currently selected cell down:
|Given Pooh Bear selected ...||... we want to select the block|
This then allows you to loop over all of the cells selected (perhaps to process the scores for each character?).
The macro shown above is the VBA equivalent of holding the SHIFT key down while you press CTRL + DOWN ARROW.
We've already seen the Range command once, with a single argument:
'select a cell by reference
However, you can also use this command with two arguments, in which case VBA will take these to be the corners of the rectangle of cells that you want to select.
In our example, we want to select from the currently selected character (let's say this is Pooh Bear) down to the bottom of the block:
We want to select the block of cells with the two corners shown in green (ie from B5 down to B11).
To achieve this, we need a command like this:
'select the column of cells by specifying the two corners
If you run this command with B5 selected, you'll get the following:
The results of running the macro above - VBA has selected the rectangle of cells with B5 as one corner and the bottom cell in the block as the other.
Suppose that we have Tigger selected, and want to select the block of data shown in orange:
Given that we've got cell B11 selected, we want to select up and to the right to select the orange area. A complication is that we want to exclude the titles row.
A macro to achieve this could be:
'select from the current cell:
'- up to the top of the block, then down 1
'- right one cell
ActiveCell.End(xlUp).Offset(1, 0), _
Notice how you can move each of the corners of the rectangle to anywhere you want on screen - provided that you specify two valid corners, VBA will always select the block of data they mark out.
An alternative way to select a block of data is to use CurrentRegion:
'push out in all directions to select a block of data
This method will select the entire block of cells of which the active cell is a part:
|Given the green active cell ...||... the above macro selects the block|
While the above looks temptingly simple, it suffers from two problems:
To illustrate the second point, supposing that you have typed a space into any of the cells shown shaded below:
The CurrentRegion method keeps going until it finds completely blank rows and columns. Woe betide you if there's some hidden content in the cells shown shaded here!
Because of the unpredictability of CurrentRegion, we'd recommend avoiding it and using the more complicated Range technique shown above.
At this point we've reached the end of the most useful techniques, but we still have a few other commands you can use! Or you could go to consider booking a place on one of our VBA courses or Excel courses ...
|Parts of this blog|
25 Aytoun Street