BLOGS BY TOPIC
BLOGS BY AUTHOR
BLOGS BY YEAR
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.
- Selecting Cells with Excel VBA Macros - Relative
- Moving Around a Spreadsheet in VBA - Offset
- Selecting to the End of a Block in Excel VBA macros
- Selecting a Block of Cells using Range (this blog)
- Resizing Blocks of Cells in Excel VBA macros
- Selecting Entire Columns or Rows in Excel VBA macros
Posted by Andy Brown on 16 August 2011
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Selecting a Block of Cells using Range
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.
The Range Command with Two Arguments
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.
Another Example of using Range to Select Blocks of Data
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 - the CurrentRegion
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:
- The macro will select the titles too - there's no easy way to omit these using CurrentRegion.
- The macro may select more than you expect (see example below).
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!