VBA Cells Excel (Table of Contents)
- #1 – How to Use CELLS Property in VBA?
- #2 – How to Use CELLS Property with Range Object?
- #3 – Cells Property with Loops
VBA Cell References
You don’t need any special introduction about what is a VBA cell. In VBA concepts, cells are also the same no different from normal excel cell. Follow this article to have more knowledge of VBA cells concept.
What is VBA Range & VBA Cell?
I am sure this is the question running in your mind right now. In VBA Range is an object but Cell is a property in excel sheet. In VBA we have two ways of referencing a cell object one is through Range and another one is through Cells.
For an example, if you want to reference as cell C5 you can use two methods to refer the cell C5.
Using Range Method: Range (“C5”)
Using Cells Method: Cells (5, 3)
Similarly, if you want to insert value “Hi” to C5 cell then you can use below code.
Using Range Method: Range (“C5”).Value = “Hi”
Using Cells Method: Cells (5, 3).Value = “Hi”
Now if you want to select multiple cells we can only select through Range object. For example, if I want to select cells from A1 to A10 below is the code.
Code: Range (“A1:A10”).Select
But unfortunately, we can only reference one cell at a time by using CELLS property. We can use Cells with Range object like the below
Range (“A1:C10”).Cells(5,2) means in the range A1 to C10 fifth row and second column i.e. B5 cell.
The Formula of CELLS Property in VBA
Take look at the formula of CELLS property.
- Row Index: This nothing but which row we are referencing.
- Column Index: This nothing but which column we are referencing.
- Cells (1, 1) means A1 cell, Cells (2, 1) means A2 cell, Cells (1, 2) means B1 cell.
- Cells (2, 2) means B2 cell, Cells (10, 3) means C10 cell, Cells (15, 5) means E15 cell.
#1 – How to Use CELLS Property in VBA?
Now I will teach you how to use these CELLS property in VBA.
Assume you are working in the sheet name called Data 1 and you want to insert a value “Hello” to the cell A1.
Below code would do that for you.
Sub Cells_Example() Cells(1, 1).Value = "Hello" End Sub
Now I will go to the sheet name called Data 2 and will run the code. Even there it will insert the word “Hello”.
Actually, we can combine the CELLS property with particular sheet name as well. To refer particular sheet use the WORKSHEET object.
Worksheets(“Data 1”).Cells(1,1).Value = “Hello”
This will insert the word “Hello” to the sheet “Data 1” irrespective of which sheet you are in.
#2 – How to Use CELLS Property with Range Object?
Actually, we can use CELLS property with RANGE object. For example, look at the below code.
For better understanding, I have entered a few numbers in the excel sheet.
The above code Range(“C2:E8”).Cells(1, 1).Select says in the range C2 to E8 select the first cell. Run this code and see what happens.
Sub Cells_Example() Range("C2:E8").Cells(1, 1).Select End Sub
It has selected the cell C2. But Cells (1, 1) means A1 cell, isn’t it?
The reason it has selected the cell C2 because using range object we have insisted on the range as C2 to E8, so Cells property treats the range from C2 to E8, not from regular A1 cell. In this example, C2 is the first row and first column, so Cells (1, 1).select means C2 cell.
Now I will change the code to Range(“C2:E8”).Cells(3, 2).Select and see what happens.
Run this code and check which cell actually it will select.
Sub Cells_Example() Range("C2:E8").Cells(3, 2).Select End Sub
It has selected the cell D4 i.e No 26. Cells (3,2) means starting from C2 cell moved down by 3 rows and move 2 columns to the right i.e. D4 cell.
#3 – Cells Property with Loops
CELLS property with loops has a very good relationship in VBA. Let’s look at the example of inserting serial numbers from 1 to 10 using FOR LOOP. Copy and paste the below code to your module.
Sub Cells_Example() Dim i As Integer For i = 1 To 10 Cells(i, 1).Value = i Next i End Sub
Here I have declared the variable I as an integer.
Then I have applied FOR LOOP with I = 1 to 10 i.e. loop needs to run for 10 times.
Cells(i,1).value = i
This means that when the loop first runs the value of “I” will be 1, so wherever the value of “I” is 1 i.e. Cell(1,1).value = 1
When the loop returns the value of “I” for the second time, it is 2, so wherever the value of “I” is, it is 2. i.e. Cell(2,1).value = 2
Like this loop will run for 10 times and insert I value from A1 to A10.
Things to Remember in VBA Cells
- CELLS is a property but RANGE is an Object. We can use property with objects but not object to the property.
- When the range is supplied cells will consider only that range, not the regular range.
- Cells (1, 2) is B1 cell, similarly Cells (1, ”B”) is also B1 cell.
This has been a Guide to VBA Cells. Here we learn how to use VBA Cell Reference Property with Range Object along with practical examples and downloadable excel templates. Below you can find some useful excel VBA articles –