Excel VBA Selection Property
VBA Selection is used for selecting the range from excel worksheet. We can do anything and whatever with the selected range. But while using VBA Selection, we will not be seeing the list of functions we want to perform from where we can choose any as per our need. For this, we need to type that thing manually and it should be correct. So while using VBA Selection, we have to make sure that we will be using the correct function to avoid any kind of error.
How to Use Selection Property in Excel VBA?
Below are the different examples to use Selection property in excel by using VBA code.
Excel VBA Selection – Example #1
In this example, we will see a very simple method of applying VBA Selection. For this follow the below steps:
Step 1: Go to the VBA window and open a new Module from the list of Insert menu as shown below.
Step 2: Now in the newly opened module write the subcategory for VBA Selection as shown below.
Code:
Sub VBASelection() End Sub
Step 3: Select any range of cells. It can be any random or specific range of cells with the help of Select command.
Code:
Sub VBASelection() Range("A1:C3").Select End Sub
Step 4: Now, we would try to insert some text in the selected range of cell. For this in the next line of code write Selection.Value. This is used for selecting the cells for putting the value in.
Code:
Sub VBASelection() Range("A1:C3").Select Selection.Value End Sub
Step 5: At last, we will write any text which we would like to see in the selected range of cell. Let that text be “Excel VBA Selection”.
Code:
Sub VBASelection() Range("A1:C3").Select Selection.Value = "Excel VBA Selection" End Sub
Step 6: Now run the code by clicking on the Play button which is placed below the menu bar as shown below.
We will see, from cell A1 to C3, our selected text value has been inserted and the whole is selected as well.
Excel VBA Selection – Example #2
In this example we will see, how Offset function will work along with VBA Selection. For this, follow the below steps:
Step 1: Write the subcategory of VBA Selection as shown below.
Code:
Sub VBASelection2() End Sub
Step 2: Select the range of cell as per your need or else we can keep off using the same range of cells which we had seen in the above example.
Code:
Sub VBASelection2() Range("A1:C3").Select End Sub
Step 3: Now choose Offset function along with Selection as shown below. By this Offset function, we would try to offset or move the selection from the next matrix.
Code:
Sub VBASelection2() Range("A1:C3").Select Selection.Offset End Sub
Step 4: After that select the offset location where we want to move the selection box from range cells A1 to C3. Let’s try to move it by 1 row and 1 column away from the current position.
Code:
Sub VBASelection2() Range("A1:C3").Select Selection.Offset(1, 1).Select End Sub
Step 5: Now again run the code by pressing F5 key or by clicking on Play Button.
We will see, now our new selection will be from cell B2 to D4 as shown below. And we had used Select command so that range is in a current selected mode.
Excel VBA Selection – Example #3
In this example, we will see how could we use and change the cell’s interior color with the help of VBA Selection. For this, follow the below steps:
Step 1: Write the subcategory of VBA Selection or in any name as per your choice as shown below.
Code:
Sub VBASelection3() End Sub
Again, we will keep on using the same range of cells as we have seen previously with the same inserted text.
Step 2: Select the range of required cell, which is from cell A1 to C3 here.
Code:
Sub VBASelection3() Range("A1:C3").Select End Sub
Step 3: Now for changing the interior color of any cell, we need to use Interior.Color function together along with Selection. This will allow us to use all available vb Color we have in VBA.
Code:
Sub VBASelection3() Range("A1:C3").Select Selection.Interior.Color End Sub
Step 4: Here we have a variety of colors but all are BASE COLORS. To select any base color we will use vb followed by the name of the color. Let’s consider the interior color to be Green as shown below.
Code:
Sub VBASelection3() Range("A1:C3").Select Selection.Interior.Color = vbGreen End Sub
Step 5: Now again run the code by pressing F5 key or by clicking on the Play Button.
We will see, our selected range of cells are colored as Green.
Excel VBA Selection – Example #4
In this example, we will insert a text in any range of cells and simultaneously we will change the font color for those cell text as well. For this, follow the below steps:
Step 1: Write the subcategory of VBA Selection as shown below.
Code:
Sub VBASelection4() End Sub
Step 2: Again select the range of cells where we want to see all this happening. Here, we are again considering the same range.
Code:
Sub VBASelection4() Range("A1:C3").Select End Sub
Step 3: Again insert any text as we have seen in example-1. We will again use the same line of code here.
Code:
Sub VBASelection4() Range("A1:C3").Select Selection.Value = "Excel VBA Selection" End Sub
Step 4: Now with the help of Selection use Font.Color function together as shown below. By this, we would be able to select the color which we want to give to selected cell fonts.
Code:
Sub VBASelection4() Range("A1:C3").Select Selection.Value = "Excel VBA Selection" Selection.Font.Color End Sub
Step 5: Again, we will use VBA base colors for cell font colors. Let’s use Red color this time starting with vb as shown below.
Code:
Sub VBASelection4() Range("A1:C3").Select Selection.Value = "Excel VBA Selection" Selection.Font.Color = vbRed End Sub
Step 6: Now again run the code by pressing F5 key or by clicking on Play Button.
We will see, in the selected range of cells we will have our text inserted and the color those cells are now Red from default Black.
Pros & Cons of Excel VBA Selection
- This the direct way of selecting any function.
- This is applicable to all type of functions.
- VBA Selection is a much easier way of coding as compared to other forms.
- As it does not allow the list of possible functions after it, so it becomes little complex to use VBA Selection.
Things to Remember
- Make sure you use exact function after VBA Selection, to avoid getting error.
- It is easy and possible to use complex code structure VBA Selection.
- Once implementation is done, save the file in Macro enabled excel so that you will not lose written code ever.
Recommended Articles
This is a guide to VBA Selection. Here we discuss how to use selection property in Excel by using VBA code along with practical examples and downloadable excel template. You can also go through our other suggested articles –