VBA Offset

Excel VBA Offset Function

VBA OFFSET is a function that helps us to navigate from one cell to another cell bypassing a specified number of rows to the down or top from the given cell and a specified number of columns to the right or left from the reference point cell.

For the VBA OFFSET function, we need to give the starting cell or reference cell by using the RANGE object, then we need to enter the number of rows to move down or up, and the number of columns to move right or left. Based on the numbers provided for the rows and columns, the VBA OFFSET function will take us to the corresponding cell from the starting cell.

If you have already used the OFFSET excel function in the worksheet, then it will be much easier for you to understand the same in VBA as well.

Key Takeaways
  • VBA OFFSET helps us to navigate to a specific cell by skipping the required number of rows and columns.
  • OFFSET can select or copy the cells based on the method that we choose along with the OFFSET property.
  • For the OFFSET function starting cell reference should be given outside the OFFSET function, unlike the worksheet function OFFSET.
  • By using the OFFSET, we can select or copy multiple cells as well.
  • An active cell is nothing, but the cell which is selected in the worksheet.

Offset With Range Object

If we have used the OFFSET function in the worksheet function, then we need to give the reference cell within the OFFSET function itself. However, in VBA, the case is different. In VBA, we need to give the reference cell with the RANGE object; then, we can access the OFFSET function.

First, we need to give the starting cell address by using the RANGE object as shown below:

VBA Offset - 1

Next, enter the dot and type the word OFFSET. We can see the IntelliSense list shows the OFFSET function in its library.

VBA Offset - 2

Likewise, VBA OFFSET and RANGE go hand in hand.


Excel VBA – All in One Courses Bundle (35+ Hours of Video Tutorials)

If you want to learn Excel and VBA professionally, then ​Excel VBA All in One Courses Bundle​ (35+ hours) is the perfect solution. Whether you’re a beginner or an experienced user, this bundle covers it all – from Basic Excel to Advanced Excel, Macros, Power Query, and VBA.

Syntax Of Offset In Excel VBA

The following is the syntax of the OFFSET function in VBA.

Range(“Cell Reference”).Offset([Row Offset], [Column Offset])

  • Range(“Cell Reference”): To access the OFFSET function in VBA, first, we need to provide the cell’s address using the RANGE object.
  • [Row Offset]: The number of rows we need to offset. If we provide a positive number, it will move down from the given cell and if we provide a negative number, then, it will move up from the given cell.
  • [Column Offset]: The number of columns we need to offset. If we provide a positive number, it will move right from the given cell and if we provide a negative number, then, it will move left from the given cell.

Examples

Example #1 – Select a Cell using the OFFSET Function in VBA

We will use the following data for all the examples in this article.

Offset Function in VBA - Example 1

Assume we need to select cell B6 from cell A1. We will use the OFFSET function along with the RANGE object in VBA.

  • Step 1: Enter the RANGE object.
Example 1 - Step 1
  • Step 2: Enter the starting cell address as A1 in double quotes and close the parenthesis.
Example 1 - Step 2
  • Step 3: Enter the dot and choose the OFFSET function from the IntelliSense list.
Example 1 - Step 3
  • Step 4: The first argument of the OFFSET function is the row offset i.e., how many columns do we need to move down. Since, we need to select cell B6, we need to move down 5 rows from cell A1. Enter 5 for the rows offset argument.
Example 1 - Step 4
  • Step 5: Next, for the column offset argument, we need to mention how many columns do we need to move right from column A. Since we need to move to column B, it will be 1.
Example 1 - Step 5
  • Step 6: Now, we need to choose the select method of the offset property.
Offset Function in VBA - Example 1 - Step 6a

Now, let’s run this code and it should select cell B6.

Offset Function in VBA - Example 1 - Step 6b

Sub Example1_Offset()

Range("A1").Offset(5, 1).Select

End Sub

Example #2 – OFFSET with Negative Numbers

We can move up rows and move left columns as well with the OFFSET function in VBA. For example, assume we are in cell C5. From this cell, we want to select cell A2.

  • Step 1: First, reference the starting cell using the RANGE object i.e., cell C5.
Offset Function in Excel VBA - Example 2 - Step 1
  • Step 2: Enter the OFFSET function by entering the dot after the RANGE object parenthesis.
Example 2 - Step 2
  • Step 3: From cell C5, we need to select cell A2. So, we need to move up 3 rows from row number 5. Whenever we need to move up rows, we need to provide the number of rows to move up in negative numbers.
Example 2 - Step 3
  • Step 4: From column C, we need to go to column A. When we need to move a column to the left of the current column (i.e., column C), we need to enter the number of columns to move left in negative numbers. In this case, we need to move to the left 2 columns i.e., -2.
Offset Function in Excel VBA - Example 2 - Step 4a

This will select cell A2 from cell C5.

Offset Function in Excel VBA - Example 2 - Step 4b

Sub Example2_Offset()

Range("C5").Offset(-3, -2).Select

End Sub

Example #3 – Select Range of Cells Using the OFFSET

So far, we have seen selecting a single cell using the OFFSET property in VBA. However, we can also select multiple cells using the OFFSET function in VBA.

Assume we need to select the range of cells C4:C6 from range A1:A3. Then, first, we need to give the excel cell reference like the following using the RANGE object.

Excel VBA Offset Function - Example 3 - Step 1

Next, choose the OFFSET property from the IntelliSense list.

Example 3 - Step 2

Since we need to select the cells from C4 to C6, we need to move down 3 rows and 2 columns to the right.

Excel VBA Offset Function - Example 3 - Step 3

This will select multiple ranges of cells i.e., C4:C6.

Excel VBA Offset Function - Example 3 - Step 4

Example #4 – Copy Multiple Range of Cells

Assume we need to copy the data range from A2 to D7 starting from cell A1. Then, we need to use the following code:

VBA Offset Function - Example 4 - Step 1

We have segregated the code into 3 parts to understand it better.

  • 1 – First, we have used the RANGE object along with the OFFSET function in VBA. Using the RANGE object, we have specified the starting cell as A1 and moved it down one row.
  • 2 – Secondly, we are moving down 6 rows and 3 columns to the right. This is because we have 6 rows and 3 columns in our data.
  • 3 – Since we are copying the data, we have used the COPY method to copy the given data range i.e., from A2 to D7.

Example #5 – OFFSET with CELLS Property

We can also use the OFFSET function with the CELLS property as well. For example, look at the following code.

VBA Offset - Example 5 - Step 1a

Sub Example4_Offset()

Range(Range("A1").Offset(1, 0), Range("A1").Offset(6, 3)).Copy

End Sub

Using the CELLS property, we have given the starting cell as cell A1. Then, we have used the OFFSET function to move down 2 rows and 3 columns to the right and select cell D3.

This will select cell D3.

Example 5 - Step 1b

Important Things To Note

  • OFFSET is a property or a function available with the RANGE object, CELLS property, and Active Cell Object.
  • OFFSET function requires the number of rows and columns’ number in negative numbers when we need to move up or move left from the active cell in VBA.
  • To select multiple cells, we need to start the cell reference with multiple cells with the RANGE object.

Frequently Asked Questions (FAQs)

1. Can You OFFSET a Range in Excel VBA?

Yes, we can offset a range in VBA using the OFFSET property of the RANGE object.

2. What Does ActiveCell.Offset mean in Excel VBA?

An active cell is nothing but the cell which is selected in the worksheet. For example, look at the following image:

VBA Offset - FAQ 2

In this image, the active cell is cell A1. From this cell, we want to move down 2 rows and 2 columns to the right. Then, we can use the active cell property.

ActiveCell.Offset(2,2).Select

FAQ 2 - 1

This will select cell C3.

FAQ 2 - 2

3. How to Offset Rows in Excel VBA?

If we want to offset 2 rows from the active cell, then we can use the following code.
Range(ActiveCell.Offset(1,0), ActiveCell.Offset(2,1)).Select

4. How to Offset One Cell Down in Excel VBA?

To Offset 1 cell down from the active cell, then, we can use the following code:
ActiveCell.Offset(1,0).Select

Download Template

This article must be helpful to understand the VBA Offset, with its formula and examples. You can download the template here to use it instantly.

This has been a guide to VBA OFFSET. Here we learn how to use OFFSET function in Excel VBA, with RANGE object, examples & downloadable excel template. You can learn more from the following articles –

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *