Microsoft training courses | Wise Owl - home page

Phone (01457) 858877 or email

RELATIVE SELECTION USING EXCEL VBA MACROS

Part two 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 (this article)
  3. Selecting to the End of a Block in Excel VBA macros
  4. Selecting a Block of Cells using Range
  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 | no comments

Moving Around a Spreadsheet in VBA - Offset

To move round a spreadsheet, use the Offset property.  Let's suppose that you want to go from any character to their Poohsticks score in the example below:

Example of offset method

With a character selected initially, you want to write a macro to select their score (ie the cell immediately to the right).

 

The Offset property which you need always specifies how many:

  • rows down you need to go; and
  • columns to the right you need to go.

In our case, we want to go 0 cells down and 1 column to the right:

Diagram showing OFFSET arrows

All movement using Offset is down and to the right.

 

If you forget which order the directions come in, use autocompletion:

Offset method autocompletion

When you type in the Offset property, it's obvious that rows come before columns.

 

Thus the full command for our example would be:

Sub SelectScore()

'select a character's score

ActiveCell.Offset(0, 1).Select

End Sub

That is, take the current cell, move it 0 rows down and 1 column to the right, and select the result.

Moving Up and Left

If you've been reading thus far, you'll be wondering how you can possibly go up and to the left.  The answer is to use negative numbers as the row and/or column offsets:

Sub SelectScore()

'given a character's score, select

'the character on their left

ActiveCell.Offset(0, -1).Select

End Sub

The macro above would go left one cell from the current one.

Overshooting the Edge of the Worksheet

If you try going too far in one direction, you'll generate an error.  For example, suppose you're on the Winnie the Pooh cell and run this macro:

Sub SelectScore()

'assume that we have Winnie the Pooh selected (cell B5)

'and try to go up 100 rows

ActiveCell.Offset(-100, 0).Select

End Sub

The result will be a less-than-friendly error message:

Error message if go to far

Microsoft politely telling you that you've gone beyond the edge of a worksheet

 

The Most Common Errors using Offset

The most common mistake using Offset is to forget to select the results of an offset (in the code below, we need to add .Select to the end of the command in CAPITALS, to complete the sentence):

Sub SelectScore()

'given a character, select their score (ie go right one cell)

ACTIVECELL.OFFSET(0,1)

End Sub

The other common mistake is to get the row and column offsets the wrong way round!

Now that we've learnt how to move around a worksheet, let's look at something slightly more complicated: ie going to the end of a block of cells.

 

RELATIVE SELECTION USING EXCEL VBA MACROS

Part two 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 (this article)
  3. Selecting to the End of a Block in Excel VBA macros
  4. Selecting a Block of Cells using Range
  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.

Comments on this blog

This blog currently has no comments.

All content copyright Wise Owl Business Solutions Ltd 2014. All rights reserved.