What is the Select method of Range
The Select method of VBA range can be used to select a cell or range of cells.
In the selected cells, you may perform various tasks e.g. setting the font name, making it bold/italic, etc., setting the background color of the selection, and others.
- Select single cell
- Select cells in a range
- Selecting a row
- How to select a column
- All rows selection
- Setting font based on selection
- Border color of selected cells
- Interior Color
An example of Select method to select a cell
We will select a cell by using the Select method in an empty active sheet:
Code:
Sub select_ex() Range("C3").Select End Sub
Output:
Selecting multiple cells example
In the example below, cells from A2 to D6 are selected:
Code:
Sub select_ex() Range("A2:D6").Select End Sub
Output:
Selecting a column
You may use the following code to select one column (B in that case):
Code:
Sub select_ex() Range("B:B").Select End Sub
Result:
Select multiple columns
Similarly, multiple columns can be given to be selected by using Range’s select method:
Code:
Sub select_ex() Range("B:D").Select End Sub
Result:
Selecting single row example
Single or multiple rows can be selected by providing the number of row in double-quotes.
For selecting row number 3:
Code:
Sub select_ex() Range("3:3").Select End Sub
Result:
Multiple rows selection
Selecting row number 5 to 12 in the code below:
Sub select_ex() Range("5:12").Select End Sub
Output:
Selecting non-adjacent rows, columns, cells example
Nonadjacent rows, columns, or cells can also be selected easily.
Just separate the cells, columns or rows ranges by a comma. Let us look at all of these non-contiguous selections:
Non-adjacent rows:
Code:
Sub select_ex() Range("2:4, 7:9").Select End Sub
Non-adjacent columns
Sub select_ex() Range("B:C, G:I").Select End Sub
Result:
Non-adjacent cells:
Sub select_ex() Range("A2:C6, B9:E12").Select End Sub
Result:
Even more, than two ranges can also be given:
Sub select_ex() Range("A2:C6, B9:E12, A16:D15").Select End Sub
Result:
Selecting all cells example
You may select all cells in the active sheet as follows:
Cells.Select
Select with Offset
Suppose we want to select a range omitting the heading row. You can use Offset with Select method for omitting as shown below:
Code:
Sub select_ex() Range("A1:D7").Offset(1, 0).Select End Sub
Result:
A few examples of Select usage
Now let us look at the usage of the Select method in sheets. We will set the Interior/background color of the selected range, borders, font bold, italic, etc. in the examples below.
So, let us do this one by one.
Setting the interior color of the selected range
Sub select_ex() Range("B2:E7").Select Selection.Interior.ColorIndex = 5 End Sub
Result:
Border color example in columns range
Sub select_ex() Range("D:F").Select Selection.Borders.ColorIndex = 17 End Sub
Output:
Bold text for row number 6
Sub select_ex() Range("6:6").Select Selection.Font.Bold = True End Sub
Output:
Making text italic for the Price column
Sub select_ex() Range("C:C").Select Selection.Font.Italic = True End Sub
Output:
Changing the Red Color text of the Status column (D)
Sub select_ex() Range("D:D").Select Selection.Font.Color = RGB(255, 0, 0) End Sub
output: