Excel is the most commonly used spreadsheet management software. VBA , or Visual Basic for Applications, takes it a step further and makes it easy for you to automate just about anything in Excel (check out this tutorial for a quick into to VBA). In today’s tutorial, we’re going to show you how to use the Offset function in VBA, to select a cell or a range of cells. You require basic familiarity with both Excel and VBA.
If you’re new to them, no problem, you can try out our course for VBA with Excel.
What is the Offset Function
The OffSet() returns the value of the cell, at an offset that you’ve specified. The syntax of this function looks like this:
OffSet(Cell reference, rows, columns, height, width)
Here’s what each parameter means
- Cell reference: refers to a single cell or range of cells.
- Rows: specify the number of rows away from the given cell. If the value entered is negative, the given cell shifts to the left.
- Columns: specify the number of columns away from the given cell. If value is negative, the cell shifts up.
Note that height and width are optional. They are used to specify the height and width of the returned range in cells. You can insert the OffSet() formula in any cell in the worksheet, except obviously, the cells from which you want the offset.
Combining Range() with OffSet()
The OffSet property is usually used in tandem with the Range property to specify a new location. Let’s take a look at this example
This code will select C2. The parameter RowOffSet is used to move 1 row from cell B1. Similarly, the parameter ColumnOffSet is used to move 1 column from cell B1. Note that the parameters have to be separated by a comma.
An easier way to accomplish the same result is to use a shorthand method, like we show below
Here we only specified the numbers for the row and column offset. VBA will internally “know” that the first one is for the row offset, while the second one is for the column offset. If you’d like to explore the Range() function more, you can take a look at our course on VBA macros. Now, let’s move on to some more examples.
How to Specify Only Rows
It’s possible to specify just the rows and not the columns. Here’s how you can do it.
Here we’ve omitted the column parameter and its comma. This code results in only moving 1 row down from the original cell location. It will select the entire row, not just a single cell.
How to Specify Only Columns
Similarly it’s possible to just specify the columns and omit the rows. The code will look like this:
Range("B1").Offset(, 1 ).Select
The comma is necessary for Excel to know that only column offset has been set.
How to Specify Negative OffSet
Can you guess how to do this? It’s simple enough. Here’s the code
Range("D2").Offset(-1, -1 ).Select
This makes the selection go up 1 row and 1 column from the starting position. That is from cell D2 we move up to cell C1.
How to OffSet a Range of Cells
It is also possible to specify more than one cell for the Range. Say you want to select or get the offset for a range of cells
Here, we start off with the range of cells from B1 to D4. The objective is to offset this complete range of cells by 1 row and 1 column. The new range of cells will be from C2 to E5.
Combining Active Cell with Offset
The active cell refers to the currently selected cell. Let’s check a few examples that involve active cells. Here is the code to move one cell down from the currently selected cells is
Combining Active Cell with Range() and Offset()
The following code selects a cell in addition to four more to the right which are to be copied/pasted in another location.
Take note that there is a comma after the first ActiveCell instance and a double closing parenthesis before the Copy. Let us now move on to cover simple programs that use OffSet functionality to obtain desired results.
Example 1: To Count the Rows in an Excel Worksheet
Sub CountNumRows() Dim Count1 as Long Count1 = 0 Do Count1 = Count1 + 1 ActiveCell.Offset(1, 0).Select Loop Until IsEmpty(ActiveCell.Offset(0, 1)) MsgBox "There are" Count1 "Rows" End Sub
In this program, we declared Count1 as a variable of type long. Count is incremented by 1. Then OffSet() is used to loop through the rows. When an empty row is encountered the loop terminates. The variable count1 contains the total number of rows.MsgBox() displays a pop-up box with the total number of rows.
Example 2: Combining Range with OffSet to Fill a Range of Cells with Even Numbers from 1 to 100.
Sub prog1() Dim Num Num = 0 For Row = 0 To 9 For Col = 0 To 9 Range(“A1”).Offset(Row, Col).Value = Num Num = Num + 2 Next Col Next Row End Sub
In this program, A1 is the given cell and the resultant range is from A1 to J10. Variable Num is initialized to zero and is incremented by the value 2 each time in the loop. The resultant range of cells contain each and every even number without duplicates from 1 to 100.
Hope you found the offset function useful, and can now use it in your own programs. We have just covered the tip of the iceberg. There’s lots more to it. You can check out more details with this ultimate VBA course, and if you’d like to hop over to advanced Macros we just the right course with Mr Excel!