WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 482 reviews for our classroom and online training
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.

  1. Selecting Cells with Excel VBA Macros - Relative
  2. Moving Around a Spreadsheet in VBA - Offset
  3. Selecting to the End of a Block
  4. Selecting a Block of Cells using Range (this blog)
  5. Resizing Blocks of Cells in Excel VBA macros
  6. Selecting Entire Columns or Rows in Excel VBA macros

This blog is part of our Excel macros online tutorial series.  Wise Owl also run VBA macros courses for beginners and advanced users.

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:

Single cell selected Block selected
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:

Corners of range to be selected

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:

Sub SelectBlock()

'select the column of cells by specifying the two corners

Range(ActiveCell, ActiveCell.End(xlDown)).Select

End Sub

If you run this command with B5 selected, you'll get the following:

Column of cells selected

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:

Block of data 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:

Sub SelectBlock()

'select from the current cell:

'- up to the top of the block, then down 1

'- right one cell

Range( _

ActiveCell.End(xlUp).Offset(1, 0), _

ActiveCell.Offset(0, 1)).Select

End Sub

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:

Sub SelectBlockAlternative()

'push out in all directions to select a block of data


End Sub

This method will select the entire block of cells of which the active cell is a part:

Single cell Block of cells
Given the green active cell ... ... the above macro selects the block

While the above looks temptingly simple, it suffers from two problems:

  1. The macro will select the titles too - there's no easy way to omit these using CurrentRegion.
  2. 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:

Border of block shown shaded

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 ...

This blog has 0 threads Add post