VBA Select Cell
Last Updated :
-
Blog Author :
Edited by :
Reviewed by :
Table Of Contents
Excel VBA Select Cell
In VBA, the selection one may make by a keyword method statement known as a SELECT statement. The Select statement is used with the range property method to make any selection. We will still use the range property method with the select statement and the cell reference to select any particular cell.
In Excel, we work with cells and the range of the cell. In a regular worksheet, we can select the cell by mouse or reference the cell, as simple as that. However, in VBA, it is not that straightforward. For example, if we want to select the cell A1 using VBA, we cannot simply say "A1 cell". Rather, we need to use the VBA RANGE object or CELLS property.
VBA coding is a language that specifies a way of doing tasks. For example, selecting cells in one of those tasks which we need to script in the VBA language. This article will show you how to select the cell using the VBA code.
Table of contents
How to Select Excel Cell using VBA?
Example #1 - Select Cell through Macro Recorder
To start the learning, let us start the process by recording the macro. But, first, place a cursor on a cell other than the A1 cell.
We have selected the B3 cell as of now.
Now click on the record macro button.
As soon as you click on that button, you will see a window. In this, you can give a new name or proceed with the default name by pressing the "OK" button.
Now, we are in the B3 cell, so select cell A1.
Now, stop the recording.
Click on "Visual Basic" to see what it has recorded.
Now, you will see the recording like this.
The only action we did while recording was to select cell A1. So, in VBA language, to select any cell, we need to use the RANGE object, then specify the cell name in double quotes and use the SELECT method to select the specified cell.
Example #2 - Select Cells using Range Object
By recording the Macro, we get to know how to select the cell. We need to use the object RANGE. Write on your own, type the word RANGE, and open parenthesis.
Code:
Sub Macro1() Range( End Sub
Now, it asks what the cell you want to refer to in the range, type "A1," is. Then, enter the cell address, close the bracket, and type dot (.) to see all the properties and methods available with this cell.
Type SELECT as the method since we need to select the cell.
Code:
Sub Macro1() Range("A1").Select End Sub
Place a cursor in the different cells and run this code to see how it selects cell A1.
Example #3 - Insert Values to Cells
After selecting the cell, what do we usually do?
We perform some action. One action is to enter some value. We can enter the value in two ways. One is again using the RANGE object or uses the object ActiveCell,
To insert a value using the RANGE object, refer to cell A1 by using RANGE.
This time we are inserting the value, so select VALUE property.
Code:
Sub Macro1() Range("A1").Select Range("A1").Value End Sub
To insert a value, put an equal sign and enter your value in double quotes if the value is text. If the value is numeric, you can directly enter the value.
Code:
Sub Macro1() Range("A1").Select Range("A1").Value = "Hello" End Sub
Now, press the F8 key to run the code line-by-line to understand the line of codes. For example, the first press of the F8 key will highlight the Macro name with yellow before this select B2 cell.
Upon pressing the F8 key one more time, it should insert the value "Hello" to cell A1.
We can also insert the value by using the Active Cell method.
The moment we select the cell, it becomes an active cell. So, use the Property Active cell to insert the value.
It is also the same as the last one. Using a range object makes it "explicit," and using active cells makes it "Implicit."
Example #4 - Select More than one Cell
We can also select multiple cells at a time. We need to specify the range of cells to select in double quotes. If you want to select cells from A1 to A5, below is the way.
Code:
Sub Macro2() Range("A1:A5").Select End Sub
Run this code using the F5 key or manually to show the result.
We can also select noncontiguous cells with a range object. So, for example, you can do this if you want to select cells from A1 to A5, C1 to C5, or E5 cells.
Code:
Sub Macro3() Range("A1:A5,C1:C5,E5").Select End Sub
Run this code manually or through the F5 key to show the result.
We need to start the double quote before we specify any cell and then close after the last cell.
Not only cells, but we can also select the named ranges by using the range's name.
Example #5 - Select cells by using CELLS Property
Not through the RANGE object but also the CELLS property, we can select the cells.
In the CELLS property, we need to specify the row and column numbers we select. Unlike a range method, we used A1, A5, C5, and C10 as references.
For example, CELLS (1,1) means A1 cell, and CELLS (2,5) means E2 cell. Like this, we can select the cells.
Code:
Sub Macro4() Cells(2, 3).Select End Sub
Recommended Articles
This article has been a guide to VBA Select Cell. Here, we learn how to select an Excel cell using VBA code, practical examples, and a downloadable template. Below you can find some useful Excel VBA articles: -