Cell Reference in Excel VBA (8 Examples)

We’ll use a data set with the Book Name, Book Types, and Price of some books of a bookshop called Martin Bookstore. The data set lies in the range B4:D13 of the worksheet.

Data Set to Refer to Cell Reference in Excel VBA


Method 1 – Refer to a Cell Reference by Using the Range Object in VBA in Excel

  • To access the single-cell B4, use this line of code:

Dim Cell_Reference As Range

Set Cell_Reference = Range("B4")

  • The following code selects cell B4.

VBA Code to Refer to Cell Reference in Excel VBA

  • It’ll select cell B4 in the active worksheet.

You can access a range of cells in this way.

Dim Cell_Reference As Range

Set Cell_Reference = Range("B4:D13")

  • The following code selects the range B4:D13.

Select Cells to Refer to Cell Reference in Excel VBA

  • It’ll select the range of cells B4:D13.

Select Range to Refer to Cell Reference in Excel VBA

Note: You can use the Range object directly without declaring it first, like:

Range("B4:D13").Select
  • If you want to access any cell of a worksheet that’s not active, use the name of the worksheet before the Range object.
  • To access the cell B4 of Sheet2, use:
Worksheets("Sheet2").Range("B4:D13")

Method 2 – Refer to a Cell Reference by Using the Index Numbers in VBA in Excel

  • To access the cell with row number 4 and column number 2 (B4), use:
Cells(4, 2))
  • The following code again selects cell B4 of the active worksheet.

  • It’ll select cell B4.

Note: To access any cell of an inactive worksheet, use the name of the worksheet before the cell reference.

Worksheets("Sheet2").Cells(4, 2)

Read More: How to Keep a Cell Fixed in Excel Formula


Method 3 – Refer to a Cell Reference Relative to Another Cell in VBA in Excel

  • To access the cell 1 row down and 2 columns right of the cell B4 (D5), use:
Range("B4").Offset(1, 2)

The following code selects cell D5 of the active worksheet.

VBA Code to Refer to Cell Reference in Excel VBA

It’ll select cell D5.

Selecting Cell to Refer to Cell Reference in Excel VBA

Note: To refer to any cell of a worksheet that’s inactive, use the name of the worksheet before the cell reference.

For example:

Worksheets("Sheet2").Range("B4").Offset(1, 2)

Method 4 – Refer to a Cell Reference by Using the Shortcut Notation in VBA in Excel

  • To access cell B4, use:
[B4] 
  • To access the range B4:D13, use:

[B4:D13]

The following code selects the range B4:D13.

It’ll select the range B4:D13.

Select Range to Refer to Cell Reference in Excel VBA

Note: To refer to any cell of an inactive worksheet, use the name of the worksheet before the cell reference.

For example:

Worksheets("Sheet2").[B4:D13]

Method 5 – Refer to a Named Range in VBA in Excel

  • Let’s name the range B4:D13 of the active worksheet as Book_List.

Named Range to Refer to Cell Reference in Excel VBA

  • We can refer to this Named Range by the line of code:
Range("Book_List")

The following code selects the range Book_List (B4:D13).

VBA Code to Refer to Cell Reference in Excel VBA

It’ll select the range Book_List.

Note: To access any cell of an inactive worksheet, use the name of the worksheet before the cell reference.

For example:

Worksheets("Sheet2").Range("Book_List")

Read More: How to Use Cell References in Excel Formula


Method 6 – Refer to Multiple Ranges in VBA in Excel

  • To access the ranges B4:D5, B7:D8, and B10:D11, use:
Range("B4:D5,B7:D8,B10:D11")

VBA Code to Refer to Cell Reference in Excel VBA

It’ll select the multiple ranges together.

Selecting Multiple Ranges to Refer to Cell Reference in Excel VBA

  • You can use the Union property of VBA to access multiple ranges together:
Union(Range("B4:D5"), Range("B7:D8"), Range("B10:D11"))
  • You can also access multiple Named Ranges together.
Range("Named_Range_1,Named_Range_2")
  • Put the worksheet name in front of inactive worksheets.
Worksheets("Sheet2").Range("B4:D5,B7:D8,B10:D11")

Read More: Relative and Absolute Cell Address in the Spreadsheet


Method 7 – Refer to Rows and Columns in VBA in Excel

  • To access the 4th row, use:
Rows (4)

  • It’ll select the entire 4th row.

Selecting Row to Refer to Cell Reference in Excel VBA

  • Columns(4) will access the entire 4th column.
  • To access multiple rows or columns together, use the Union property of VBA. To access the rows 4, 6, 8, and 10 together, use:
Union(Rows(4), Rows(6), Rows(8), Rows(10))

VBA Code to Refer to Cell Reference in Excel VBA

It’ll select the entire rows 4, 6, 8, and 10.

Note: Add the name of the worksheet in front in case it’s an inactive one.

Worksheets("Sheet2").Rows (4)

Read More: Difference Between Absolute and Relative Reference in Excel


Method 8 – Refer to the Whole Worksheet in VBA in Excel

  • To access the whole worksheet in VBA, use:
Cells
  • To refer to an inactive worksheet (For example, Sheet2), use:
Worksheet("Sheet2").Cells

It’ll select the whole worksheet Sheet2.

Selecting Whole Worksheet to Refer to Cell Reference in Excel VBA

Read More: Excel VBA: R1C1 Formula with Variable


Download the Practice Workbook

Download this practice book to follow along while reading this article.


<< Go Back to Cell Reference in ExcelExcel Formulas | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo