Excel VBA: Set a Range by Row and Column Number – 3 Examples

This is a quick overview.

VBA Code to Set Range by Row and Column Number in Excel


Method 1 – Set a Range by Row and Column Number Using the Cell Address in VBA

To set B4:D13, use Range(“B4:D13”).

To set the range using the VBA Range function with the name Rng (here), use the following code:

Set Rng = Range("B4:D13")

VBA Code to Set Range by Row and Column Number in Excel VBA

Output:

Add this code.

Rng.Select

Select Range to Set Range by Row and Column Number in Excel VBA

  • Run the code. B4:D13 will be selected.

Notes:

If you don’t specify the worksheet name before the range, it sets the range from the active worksheet.

To set a range from a worksheet that’s not active, specify the name of the worksheet before the range.

  • To set the range B4:D13 in Sheet1, use:
Set Rng = Worksheets("Sheet1").Range("B4:D13")

VBA Code to Set Range by Row and Column Number in Excel VBA

You can also enter the name of the workbook.

Set Rng = Workbooks("Book1").Worksheets("Sheet1").Range("B4:D13")


Method 2 – Set a Range by Row and Column Number Using the Cells Property of VBA 

  • To set B4:D13 (From Row 4, Column 2 to Row 13, Column 4), use Range(Cells(4, 2), Cells(13, 4)).
  • To set the range with the name Rng, enter the following code:
Set Rng = Range(Cells(4, 2), Cells(13, 4))

VBA Code to Set Range by Row and Column Number in Excel

Output:

  • Enter this code.
Rng.Select

Selecting Range to Set Range by Row and Column Number in Excel VBA

  • Run the code. B4:D13 is selected in the active worksheet.

Output to Set Range by Row and Column Number in Excel VBA

 Notes:

Enter the name of the worksheet and the workbook if they are not active.

Read More: VBA Range with Variable Row Number in Excel


Method 3 – Set a Range within Another Range Using the Cells Function of VBA

  • To set the range from Row 2, Column 2 to Row 3, Column 3 within B4:D13, use Range(“B4:D13”).Range(Cells(2, 2), Cells(3, 3)).
  • To set the range with the name Rng, use the following code:
Set Rng = Range("B4:D13").Range(Cells(2, 2), Cells(3, 3))

Output:

  • Enter the code.
Rng.Select

Selecting Range to Set Range by Row and Column Number in Excel VBA

  • Run the code. The range from Row 2, Column 2 to Row 3, Column 3 within the range B4:D13 is selected.

Notes:

Enter the name of the worksheet and the workbook.

Read More: VBA to Set Range in Excel


Things to Remember

The UsedRange property covers the whole range in a worksheet. Use:

Set Rng=ActiveSheet.UsedRange

It sets the used range in the active worksheet.

Or

Set Rng=Worksheets("Sheet1").UsedRange

It sets the used range in the worksheet Sheet1.

 


Download Practice Workbook

Download the practice workbook to exercise.


Related Articles


<< Go Back to Rows and Columns in Excel | 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