VBA Selection Range

Excel VBA Selection Range

After the basis stuffs with VBA, it is important to understand how to work with a range of cells in the worksheet. Once you start executing the codes practically most of the time, you need to work with a range of cells, so it is important to understand how to work with a range of cells, and one such concept is VBA “Selection of Range.” In this article, we will show you how to work with the “Selection Range” in Excel VBA.

Selection and Range are two different topics, but when we say to select the range or selection of range, it is a single concept. RANGE is an object, “Selection” is a property and “Select” is a method. People tend to confuse with these terms. It is important to know the differences in general.

VBA-Selection-Range

You are free to use this image on your website, templates etc, Please provide us with an attribution linkHow to Provide Attribution?Article Link to be Hyperlinked
For eg:
Source: VBA Selection Range (wallstreetmojo.com)

How to Select a Range in Excel VBA?

You can download this VBA Selection Range Excel Template here – VBA Selection Range Excel Template

Example #1

For example, assume you want to select the cell A1 in the worksheet then. First, we need to specify the cell address by using a RANGE object like below.

Code:

VBA Selection Range Example 1

After mentioning the cell that we need to select, put a dot to see the IntelliSense list, which is associated with the RANGE object.

VBA Selection Range Example 1-1

Form this variety of lists, chooses the “Select” method.

Code:

Sub Range_Example1()

  Range("A1").Select

End Sub
VBA Selection Range Example 1-2

Now, this code will select the cell A1 in the active worksheet.

VBA Selection Range Example 1-3

If you want to select the cell in the different worksheets, then first we need to specify the worksheet by its name. To specify the worksheet, we need to use the “WORKSHEET” object and enter the worksheet name in double-quotes.

For example, if you want to select the cell A1 in the worksheet “Data Sheet,” then first specify the worksheet just like below.

Code:

Sub Range_Example1()

  Worksheets ("Data Sheet")

End Sub

Then continue the code to specify what we need to do in this sheet. In “Data Sheet,” we need to select the cell A1, so the code will be RANGE(“A1”).Select.

Code:

Sub Range_Example1()

  Worksheets("Data Sheet").Range("A1").Select

End Sub

When you try to execute this code, we will get the below error.

VBA Selection Range Example 1-4

The reason for this is “we cannot directly supply range object and select method to the worksheets object.”

First, we need to select or activate the VBA worksheet, and then we can do whatever the things we wanted to do.

Code:

Sub Range_Example1()

  Worksheets("Data Sheet").Activate
  Range("A1").Select

End Sub

Now this will select the cell A1 in the worksheet “Data Sheet.”

VBA Selection Range Example 1-5

Example #2 – Working with Current Selected Range

Select is a different thing and working with an already selected range of cells is different. Assume you want to insert a value “Hello VBA” to the cell A1 then we can do it in two ways.

Firstly we can directly pass the VBA codePass The VBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more as RANGE(“A1”).Value = “Hello, VBA.”

Code:

Sub Range_Example1()

  Range("A1").Value = "Hello VBA"

End Sub

What this code will do is this will just insert the value “Hello VBA” to the cell A1 irrespective of which cell is currently selected.

Example 2

Look at the above result of the code. When we execute this code, it has inserted the value “Hello VBA,” even though the currently selected cell is B2.

Secondly, we can insert the value to the cell by using the “Selection” property. For this first, we need to select the cell manually and execute the code.

Code:

Sub Range_Example1()

  Selection.Value = "Hello VBA"

End Sub

What this code will do is this will insert the value “Hello VBA” to the currently selected cell. For example, look at the below example of execution.

Example 2-1

When I executed the code, my current selected cell was B2, and our code inserted the same value to the currently selected cell.

Now I will select the cell B3 and execute, there also, we will get the same value.

Example 2-2

One more thing we can do with the “selection” property is we can insert value to more than one cell as well. For example, I will select the range of cells from A1 to B5 now.

Example 2-3

Now, if I execute the code, for all the selected cells, we will get the value as “Hello VBA.”

Example 2-4

So, the simple difference between specifying cell address by RANGE object and Selection property is, in Range object code will insert value to the cells which are specified explicitly.

But in the Selection object, it doesn’t matter which cell you are in. It will insert the mentioned value to all the selected cells.

Things to Remember Here

  • We cannot directly supply the select method under Selection property.
  • RANGE is an object, and selection is property.
  • Instead of range, we can use CELLS property.

Recommended Articles

This has been a guide to VBA Selection Range. Here we learn how to select a range in excel VBA along with examples and download an excel template. Below are some useful excel articles related to VBA –

  • 3 Courses
  • 12 Hands-on Projects
  • 43+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>