VBA Cells Excel | How To Use Cell Reference Property With Range ...
Có thể bạn quan tâm
Trending Courses
Course Categories
Certification Programs
Free Courses
View All Courses.VBA Resources
- E-Books
- Career Guides
- Interview Prep Guides
- Free Practice Tests
- Excel Cheatsheets
- Basic Operations
- Selection and Manipulation
- Advanced Operations
ALL COURSES@ADDITIONAL50% OFF
UNLOCK DEAL VBA CellsPublication Date :
15 Mar, 2019
Blog Author :
Jeevan A Y
Edited by :
Ashish Kumar Srivastav
Reviewed by :
Dheeraj Vaidya, CFA, FRM
Share
Table Of Contents
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 cells. Follow this article to have more knowledge of the VBA cells concept.
Table of contents
- VBA Cell References
- What is VBA Range & VBA Cell?
- The Formula of CELLS Property in VBA
- #1 - How to Use CELLS Property in VBA?
- #2 - How to Use CELLS Property with Range Object?
- #3 - Cells Property with Loops
- Things to Remember in VBA Cells
- Recommended Articles
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 an excel sheet. In VBA, we have two ways of referencing a cell object one through Range, and another one is through Cells.
For example, if you want to reference cell C5, you can use two methods to refer to 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 the 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 the 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 a Range object like the below.
Range (“A1: C10”).Cells(5,2) mean in the range A1 to C10 fifth row and second column i.e., B5 cell.
The Formula of CELLS Property in VBA
Take a 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.
The Below code would do that for you.
Sub Cells_Example() Cells(1, 1).Value = "Hello" End SubResult:
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 a particular sheet name as well. To refer a 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 a RANGE object. For example, look at the below code.
Range("C2:E8").Cells(1, 1).SelectFor 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 SubIt 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 SubIt has selected the cell D4 i.e., No 26. Cells (3,2) mean 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 SubHere I have declared the variable I as an integer.
Then I have applied FOR LOOP with I = 1 to 10 i.e., and the loop needs to run 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
This loop will run for 10 times and insert I value from A1 to A10.
Things to Remember in VBA Cells
- CELLS is property, but the 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.
Recommended Articles
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 -
- Select Cell in VBA
- Use VBA Range Function
- VLookup in Excel VBA
- Excel VBA Range Cells
Từ khóa » Visual Basic Excel Range Cells
-
Range.Cells Property (Excel) - Microsoft Docs
-
Range Object (Excel) | Microsoft Docs
-
Excel VBA Ranges And Cells
-
Hướng Dẫn Sử Dụng Hàm CELLS Và RANGE để Lấy Giá Trị ô VBA ...
-
Đối Tượng Range Trong Excel VBA - Viblo
-
VBA Code - Excel Cells & Ranges
-
The Complete Guide To Ranges And Cells In Excel VBA
-
Range Object In Excel VBA - Easy Dim And Set
-
Understanding Excel Cells Vs. Range Functions In VBA - MakeUseOf
-
Excel VBA Range Object: 18 Useful Ways Of Referring To Cell Ranges
-
Using For Each In Excel Range - Code VBA
-
Working With Range And Cells In VBA - Excel Champs
-
Working With Cells And Ranges In Excel VBA (Select, Copy, Move ...