Excel VBA Select 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 cellReference The CellCell reference in excel is referring the other cells to a cell to use its values or properties. For instance, if we have data in cell A2 and want to use that in cell A1, use =A2 in cell A1, and this will copy the A2 value in A1.read more, 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 objectVBA RANGE ObjectRange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more or CELLS property.
VBA codingVBA CodingVBA 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 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.
You are free to use this image on you website, templates, etc., Please provide us with an attribution linkHow to Provide Attribution?Article Link to be HyperlinkedFor eg:Source: VBA Select Cell (wallstreetmojo.com)
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 macroRecording The MacroRecording macros is a method whereby excel stores the tasks performed by the user. Every time a macro is run, these exact actions are performed automatically. Macros are created in either the View tab (under the “macros” drop-down) or the Developer tab of Excel. read more. But, first, place a cursor on a cell other than the A1 cell.
We have selected the B3 cell as of now.
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
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.
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.
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.
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.
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.
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.
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: –
- VBA Range CellsWhat are Clear Contents in Excel VBA?Text Box in VBADo Until Loop in VBA