Excel VBA Range Function (with 13 Examples)

Overview of Excel VBA Range Function


Introduction to Excel VBA Range Function

Function Objective: The Excel VBA Range function references a range in a worksheet.

Syntax:

Range(Cell1,[Cell2])

Excel VBA Range function syntax


Referencing Cells Using Excel VBA Range Function

We can use the Range function to reference a single cell or a range of cells.


1 – Referencing Single Cell

To refer to a single cell, use the following syntax:

Range(“D5”)

2 – Referencing Range of Cells

We can reference a range of cells in a couple of ways.

2.1 – Using Cells Property

To reference a range of cells, use either of the following syntaxes:

Range("B5:D10")
Range("B5", "D10")

The above syntax refers to the range B5 to D10.


2.2 – Using Offset Property

We can also use the Offset property to refer to a range.

The following syntax references cell D10 from cell B5:

Range("B5").Offset(4,3)

Read More: VBA to Set Range in Excel


3 – Referencing Entire Row

The following syntax references the entire row 4.

Range("4:4")

4 – Referencing Entire Column

To reference column C we can use the following syntax:

Range("C:C")

5 – Referencing Named Range

To refer to a range named Sales_Data, we can use the following syntax:

Range("Sales_Data")

Examples of Using Excel VBA Range Function


Example 1 – Selecting Cells

1.1 – Single Cell

STEPS:

  • Go to Developer > Visual Basic.

Go to Developer tab and then visual basic

  • Insert a new module by going to Insert > Module.

Insert a new module

  • Insert the following code in the module:
Sub Select_Single_Cell()
    Sheets("Sheet1").Range("B5").Select
End Sub

Insert code to select a single cell

  • Run the code by pressing F5 on your keyboard or clicking on Run.

This will select cell B5 in the worksheet.

Run code to select a single cell with Excel VBA Range function


1.2 – Entire Column

To select an entire column, use the following code:

Sub Select_Entire_Column()
    Sheets("Sheet1").Range("B:B").Select
End Sub

Insert code to select column B with Excel VBA Range function

Running the above code selects column B in the worksheet.

select column by running the code


1.3 – Entire Row

The following code selects row 5 in the worksheet:

Sub Select_Entire_Row()
    Sheets("Sheet1").Range("5:5").Select
End Sub

Insert code to select entire row with Excel VBA Range function

  • Run the above code to select row 5.

select entire row by running the code

Read More: Excel VBA: Set Range by Row and Column Number


1.4 – A Range of Cells

To select a range of cells, use the following code:

Sub Select_Two_Different_Ranges()
    Sheets("Sheet1").Range("B5:D14").Select
End Sub

Insert code to select a range with Excel VBA Range function


1.5 – Non-Adjacent Cells

To select a range of non-adjacent cells, use the following code:

Sub Select_Non_Adjacent_Cells()
    Sheets("Sheet1").Range("B5,B7,D10").Select
End Sub

Insert code to select non-adjacent cells


1.6 – Two Different Ranges

To select two different ranges, use the following code:

Sub Select_Two_Different_Ranges()
    Sheets("Sheet1").Range("B5:B14,D5:D14").Select
End Sub

Insert code to select two different ranges with Excel VBA Range function

Modify this code to select multiple ranges.


Example 2 – Input Values

2.1 – Single Cell

The following code inputs the value Exceldemy in cell C6:

Sub Input_Values_Single_Cell()
    Sheets("Sheet1").Range("C6").Value = "Ethan Rodriguez"
End Sub

Insert and run code to insert value in a single cell

We don’t need to insert a quotation for a numerical value:

Sub Input_Values_Single_Cell()
    Sheets("Sheet1").Range("D6").Value = 1950
End Sub

Insert and run code to insert numerical value in a single cell


2.2 – Multiple Cells

Easily input a value in multiple cells or a range of cells using the following code:

Sub Input_Values_Multiple_Cell()
    Sheets("Sheet1").Range("E5:E14").Value = “July”
End Sub

Insert and run code to insert value in multiple cells


Example 3 – Merge Cells

To merge a range of cells use the following code:

Sub Merge_Cells()
    Sheets("Sheet1").Range("B2:E2").Merge
End Sub

insert code to merge cells

The above code merges cells B2, C2, D2, and E2 into a single cell.


Example 4 – Unmerge Merged Cells

To unmerge merged cells, use the following code:

Sub Unmerge_Cells()
    Sheets("Sheet1").Range("B2:E2").UnMerge
End Sub

insert code to unmerge cells with Excel VBA Range function


Example 5 – Clear Formatting from Cells

To clear formatting from a cell or a range of cells, use the following code:

Sub Clear_Formatting()
    Sheets("Sheet1").Range("B4:D14").ClearFormats
End Sub

clear formatting from cells


Example 6 – Clear a Range

Use this function to clear everything from a range:

Sub Clear_Everything()
    Sheets("Sheet1").Range("B4:D14").Clear
End Sub

The code clears all content from the range C5:D10 including formatting, formulas, values, etc.

clear contents from cells


Example 7 – Delete a Range

Delete a range using the following code:

Sub Delete_Range()
    Sheets("Sheet1").Range("D5:D14").Delete
End Sub

Insert code to delete cells


Example 8 – Find a Value Within Range

Use this function to find a specific value within a range and select the cell that contains the value:

Sub Find_and_Select_Cell()
    Dim sValue As Variant
    Dim sRange As Range
    Dim foundCell As Range
    sValue = "Ashley Williams"
    Set sRange = Range("B5:D14")
    Set foundCell = sRange.Find(sValue, LookIn:=xlValues, LookAt:=xlWhole)
    If foundCell Is Nothing Then
        MsgBox "The value '" & sValue & "' not found in the specified range."
    Else
        foundCell.Select
    End If
End Sub

insert code to find specific value within range

  • Run the code to find the value and select the cell with that value.

run code to find value and select cell

Read More: Excel VBA: Get Range of Cells with Values


Example 9 – Sort a Range of Cells

Suppose we have the following dataset. Let’s sort it by a single column and then multiple columns.

Dataset before sorting


9.1 – Single Column

To sort the dataset based on the values in cell D5, use the following code:

Sub Sort_Single_Column()
    Range("B5:D14").Sort Key1:=Range("D5"), Order1:=xlAscending, Header:=xlNo
End Sub

Sort with single column using Excel VBA Range function

The dataset looks like this after sorting.

Sorted dataset with single column


9.2 – Multiple Columns

Use the following code to sort a dataset by multiple columns:

Sub Sort_Multiple_Columns()
    Range("B5:D10").Sort _
        Key1:=Range("C5"), Order1:=xlAscending, _
        Key2:=Range("D5"), Order2:=xlAscending, _
        Header:=xlNo
End Sub

Insert code to sort with multiple columns

The dataset looks like this after sorting by multiple columns.

Sorted dataset with multiple columns


Example 10 – Copy and Paste Cell Values

To copy cell values and paste them into a specific range, use the following code:

Sub Copy_and_Paste_Values()
Range("E5:E9").Select
Selection.Copy
Range("C5").Select
Sheets("Sheet1").Paste
End Sub

Copy and paste cell values using Excel VBA Range function


Example 11 – Count Cells in Range

To count the number of cells in a range, use the following code:

Sub Count_Cells()
    Dim rng As Range
    Set rng = Range("B5:D14")
    Range("E5").Value = rng.Count
End Sub

This code counts total cells in the range B5:D14 and shows the result in cell E5.

cells count using Excel VBA Range function


Example 12 – Customize Fonts

We can customize the fonts used in a range. For example, the following code makes the fonts in the range C5:C10 bold:

Sub Custom_Font()
Range("C5:C10").Font.Bold = True
End Sub

Insert code to customize fonts


Example 13 – Sum a Range of Cells

Use the following code to sum a range of cells:

Sub Sum_Range()
    Range("B14") = WorksheetFunction.Sum(Range("B5:B10"))
End Sub

This code sums the values in cells B5 to B10 and shows the summation in cell B14.

Insert code to sum values


Things to Remember

  • Remember to save the Excel file as a macro-enabled workbook.
  • The Range function can be used in conjunction with other VBA functions to create macros that automate tasks.

Frequently Asked Questions

1. What is the Excel VBA Range function used for?

The Range function in Excel VBA is used to perform various operations on cells, such as reading or modifying values, formatting, and more.

2. Can I modify cell values using the Range function?

Yes, you can modify the cell values using the Value property of the Range object. An example is shown above.


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Maruf Niaz
Maruf Niaz

Md. Maruf Niaz, BSc, Textile Engineering, Bangladesh University of Textiles, has worked with the ExcelDemy project for 11 months. He works as an Excel and VBA Content Developer who provides easy solutions to Excel-related problems and regularly writes fantastic content articles. He has published almost 20 articles in ExcelDemy. As an Excel & VBA Content Developer for ExcelDemy, he offers solutions to complex issues. He demonstrates an enthusiastic mindset and adeptness in managing critical situations with finesse, showcasing... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo