Lesson 44 of 60 – Cells Property in VBA
73%

Cells Property in VBA

The Cells property in VBA is used to refer to worksheet cells by using row numbers and column numbers.

For example, Cells(1, 1) represents cell A1, while Cells(2, 3) represents cell C2.

The Cells property is especially useful when working with loops because row and column numbers can be stored in variables and changed dynamically.

Note: The basic syntax is Cells(row, column). The first number represents the row, and the second number represents the column.

1. What is the Cells Property?

The Cells property allows you to reference an Excel cell using its row and column numbers.

Cells(1, 1)

This represents cell A1.

Cells(2, 3)

This represents cell C2.

2. Basic Cells Syntax

The basic syntax of the Cells property is:

Cells(row, column)

For example:

Cells(5, 2)

Row number is 5 and column number is 2, so this represents cell B5.

3. Understanding Row Number

The first value inside Cells represents the row number.

Cells(4, 1)

The number 4 means row 4.

Because the second number is 1, the complete reference is cell A4.

4. Understanding Column Number

The second value inside Cells represents the column number.

Cells(2, 3)

The number 3 represents column C.

Therefore, Cells(2, 3) represents cell C2.

5. Column Numbers in Excel

When using Cells, Excel columns are represented by numbers.

Column Number
A 1
B 2
C 3
D 4
E 5

6. Writing a Value with Cells

The Value property can be used with Cells to write data.

Cells(1, 1).Value = "Hello"

This writes Hello into cell A1.

Cells(2, 2).Value = 100

This writes 100 into cell B2.

7. Reading a Value with Cells

You can also read a value from a cell.

Dim studentName As String

studentName = Cells(2, 1).Value

MsgBox studentName

The value from cell A2 is stored in the variable studentName.

8. Cells vs Range

Range normally uses Excel-style cell addresses, while Cells uses row and column numbers.

Range("A1").Value = "Hello"

The same operation using Cells is:

Cells(1, 1).Value = "Hello"

Both statements refer to cell A1.

9. Why Cells is Useful

Cells is especially useful when the row or column number changes dynamically.

Dim rowNumber As Integer

rowNumber = 5

Cells(rowNumber, 1).Value = "Rahul"

Because rowNumber is 5, Rahul is written into cell A5.

10. Cells with Variables

Both row and column numbers can be stored in variables.

Dim rowNumber As Integer
Dim columnNumber As Integer

rowNumber = 3
columnNumber = 2

Cells(rowNumber, columnNumber).Value = "Excel VBA"

This writes Excel VBA into cell B3.

11. Cells with For Loop

One of the most common uses of Cells is inside a For loop.

Dim i As Integer

For i = 1 To 10

    Cells(i, 1).Value = i

Next i

This writes numbers 1 to 10 into column A.

12. Writing Text with a Loop

You can repeatedly write text into different rows.

Dim i As Integer

For i = 1 To 5

    Cells(i, 1).Value = "Student"

Next i

The word Student is written into cells A1 through A5.

13. Cells with Nested Loops

Cells works very well with nested loops.

Dim i As Integer
Dim j As Integer

For i = 1 To 5

    For j = 1 To 3

        Cells(i, j).Value = i * j

    Next j

Next i

This processes five rows and three columns.

14. Cells with Worksheet Object

You can specify the worksheet before using Cells.

Worksheets("Students").Cells(2, 1).Value = "Rahul"

This writes Rahul into A2 of the Students worksheet.

15. Cells with Worksheet Variable

A Worksheet variable can make Cells references clearer.

Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Students")

ws.Cells(2, 1).Value = "Rahul"
ws.Cells(2, 2).Value = "ADCA"

The values are written into the Students worksheet.

16. Cells with If Statement

Cells can be used inside an If statement.

If Cells(2, 2).Value >= 40 Then

    Cells(2, 3).Value = "Pass"

Else

    Cells(2, 3).Value = "Fail"

End If

The marks in B2 are checked and the result is written into C2.

17. Cells with Formulas

The Formula property can be used with Cells.

Cells(2, 3).Formula = "=A2+B2"

This places the formula =A2+B2 into cell C2.

18. Formatting Cells

Cells can be used to change formatting.

Cells(1, 1).Font.Bold = True

This makes cell A1 bold.

Cells(1, 1).Font.Size = 16

This changes the font size of A1 to 16.

19. Changing Cell Background

The Interior property can be used with Cells.

Cells(1, 1).Interior.Color = vbYellow

This changes the background color of A1 to yellow.

20. Combining Range and Cells

Range and Cells can be combined to create a dynamic range.

Range(Cells(1, 1), Cells(5, 3)).Value = 0

This represents A1:C5 and writes zero into all cells.

This technique is useful when the starting or ending row is stored in a variable.

21. Creating a Dynamic Range

Variables can be used to create a dynamic range.

Dim lastRow As Long

lastRow = 10

Range(Cells(2, 1), Cells(lastRow, 3)).Font.Bold = True

This makes the range A2:C10 bold.

22. Finding the Last Used Row

Cells is commonly used to find the last used row in a column.

Dim lastRow As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row

MsgBox lastRow

This finds the last non-empty row in column A.

23. Finding the Next Empty Row

After finding the last used row, you can calculate the next empty row.

Dim nextRow As Long

nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

Cells(nextRow, 1).Value = "New Student"

This adds New Student to the next available row in column A.

24. Cells for Student Data Entry

Cells is very useful for dynamic student data-entry systems.

Dim nextRow As Long

nextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1

Cells(nextRow, 1).Value = "ST101"
Cells(nextRow, 2).Value = "Rahul"
Cells(nextRow, 3).Value = "ADCA"

The student information is entered into the next available row.

25. Practical Student Result Example

The following example checks the marks of several students.

Dim i As Integer

For i = 2 To 10

    If Cells(i, 2).Value >= 40 Then

        Cells(i, 3).Value = "Pass"

    Else

        Cells(i, 3).Value = "Fail"

    End If

Next i

Column B contains marks and column C receives the result.

26. Common Mistakes with Cells

Beginners commonly make these mistakes:

  • Reversing the row and column numbers.
  • Using zero as a row or column number.
  • Using the wrong worksheet.
  • Forgetting that columns use numbers in Cells.
  • Overwriting existing worksheet data.
  • Using incorrect loop limits.
  • Using unqualified Cells references in larger projects.

27. Practical Data Entry Example

The following procedure enters student information into the next available row.

Sub AddStudent()

    Dim ws As Worksheet
    Dim nextRow As Long

    Set ws = ThisWorkbook.Worksheets("Students")

    nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1

    ws.Cells(nextRow, 1).Value = "ST101"
    ws.Cells(nextRow, 2).Value = "Rahul"
    ws.Cells(nextRow, 3).Value = "ADCA"

    MsgBox "Student Added Successfully"

End Sub

28. Best Practices for Cells Property

  • Remember the syntax Cells(row, column).
  • Use meaningful variables for row and column numbers.
  • Qualify Cells with a worksheet in larger programs.
  • Use Long for row numbers when processing large worksheets.
  • Use Cells when row or column numbers need to change dynamically.
  • Combine Range and Cells when creating dynamic ranges.
  • Test loops carefully before processing important data.

29. Complete Cells Property Workflow

A typical Cells workflow is:

  1. Select or reference the required worksheet.
  2. Determine the row number.
  3. Determine the column number.
  4. Reference the cell using Cells(row, column).
  5. Read, write, calculate, or format the cell.
  6. Use loops when multiple cells need to be processed.
Dim ws As Worksheet
Dim i As Long

Set ws = ThisWorkbook.Worksheets("Students")

For i = 2 To 10

    ws.Cells(i, 1).Value = "Student " & i

Next i

30. Complete Understanding of Cells Property

The Cells property is one of the most useful ways to reference Excel cells when writing dynamic VBA programs.

The basic syntax is:

Cells(row, column)

For example:

Cells(1, 1).Value = "Student Name"
Cells(1, 2).Value = "Marks"
Cells(1, 3).Value = "Result"

This writes headings into A1, B1, and C1.

Cells becomes especially powerful when combined with variables and loops:

Dim i As Long

For i = 2 To 10

    If Cells(i, 2).Value >= 40 Then

        Cells(i, 3).Value = "Pass"

    Else

        Cells(i, 3).Value = "Fail"

    End If

Next i

Because row numbers can change dynamically, Cells is commonly used in data-entry systems, student result systems, reports, searches, and other Excel automation projects.

The next lesson will introduce Rows and Columns in VBA and explain how to work with complete rows and columns programmatically.

📌 Key Points

  • The Cells property references cells using row and column numbers.
  • The syntax is Cells(row, column).
  • Cells(1, 1) represents A1.
  • Cells(2, 3) represents C2.
  • Cells can read and write worksheet values.
  • Cells works very well with loops and variables.
  • Range and Cells can be combined to create dynamic ranges.
  • Cells can be used to find the last used or next empty row.
  • Worksheet-qualified Cells references are safer in larger projects.
  • Cells is very useful for dynamic Excel VBA automation.

🧠 Quick Quiz

Question: Which cell is represented by Cells(2, 3) in VBA?