How to Use VBA for Each Row in a Range in Excel

In this article, we will demonstrate how to use Excel VBA to iterate through each row in a range effectively, where the rows contain either single or multiple columns.


Use VBA for Each Row in a Range in Excel (Quick View)

VBA For Each Row in a Range with Multiple Columns


First, we’ll discuss how to use VBA to iterate through each row of a range that contains a single column, then we’ll cover the process for multiple columns.


Method 1 – Using VBA for Each Row in a Range with a Single Column

To use VBA to iterate through each row in a range, we’ll use a for-loop.

For example, if the range is B4:B13, we can use:

For Each i In Range("B4:B13").Rows

Here, the variable i will hold each row of the range B4:B13 one by one.

As each row in the range contains a single column, we can use i directly to access the value of that row. For example, to display the value of each row in a message box one by one, we can use:

MsgBox i

We end the for-loop thereafter with the line:

Next i

VBA Code to Use VBA for Each Row in a Range in Excel


Method 2 – Using VBA for Each Row in a Range with Multiple Columns

If the range contains multiple columns, we can’t access the value from each row directly.

Instead, start the same for-loop in the previous Method. For example, if the range is B4:D13, use:

For Each i In Range("B4:D13").Rows

Since we can’t access i directly, as with each iteration it contains values from 3 columns (B, C, and D), to access a specific value each iteration we have to specify the column number of that value. We can use the Cells property of VBA for this purpose.

For example, to display the value of the 3rd column (Column D) one by one, use:

MsgBox i.Cells(1, 3)

And, of course, end the iteration with a Next statement thereafter.

Next i

VBA For Each Row in a Range with Multiple Columns

Read More: Loop through a Range for Each Cell with Excel VBA


How to Use VBA for Each Row in a Range in Excel: 4 Practical Examples

Let’s work through a few examples of using VBA to iterate through each row of a range in a worksheet.

Suppose we have a worksheet with the names of some students and their marks in Physics and Chemistry.

Data Set to Use VBA for Each Row in a Range in Excel

We’ll use use this dataset to illustrate the examples.


Example 1 – VBA to Collect Data from Each Row of a Single Column of a Given Range

Let’s gather the marks in Physics of all the students (C4:C13) in a new range F4:F13 with VBA. As the range consists of a single column, we can access the value of each row directly in this case.

The VBA code is:

VBA Code:

Sub Collecting_Data_from_a_Single_Column()

Output_Row = 1

For Each i In Range("C4:C13").Rows
    Range("F4:F13").Cells(Output_Row, 1) = i
    Output_Row = Output_Row + 1
Next i

End Sub

VBA Code to Use VBA for Each Row in a Range in Excel

Output:

Running this code will collect the marks in Physics of each student (C4:C13) and copy them to the range F4:F13.

Output of Using VBA for Each Row in a Range in Excel

Read More: How to Use VBA for Each Cell in Range in Excel


Example 2 – VBA to Collect Data from Each Row of a Single Column of a Given Range with a Criterion

Now, we’ll collect data from a column of a given range with a criterion: the marks in Physics of the students (C4:C13) who achieved higher marks than 60. We’ll store these values in a new range (F4:F13)

As the range consists of a single column again, we can access the value of each row directly.

The VBA code is:

VBA Code:

Sub Collecting_Data_from_a_Single_Column_with_Criterion()

Output_Row = 1

For Each i In Range("C4:C13").Rows
    If i > 60 Then
        Range("F4:F13").Cells(Output_Row, 1) = i
        Output_Row = Output_Row + 1
    End If
Next i

End Sub

Output:

Running this code will collect the marks in Physics of the students (C4:C13) who achieved a mark of more than 60, and copy them to the range F4:F13.

Output of Using VBA for Each Row in a Range in Excel


Example 3 – VBA to Collect Data from Each Row of Multiple Columns of a Given Range

This time we’ll collect data from multiple columns of a given range.

Let’s collect the marks in Physics of all the students along with their names (B4:C13) in a new range (F4:G13). Since the output range involves two columns (B4:C13), we’ll use the Cells property of VBA to collect the values.

The VBA code is:

VBA Code:

Sub Collecting_Data_from_Multiple_Columns()

Row_Number = 1

For Each i In Range("B4:C13").Rows
    Range("F4:G13").Cells(Row_Number, 1) = i.Cells(1, 1)
    Range("F4:G13").Cells(Row_Number, 2) = i.Cells(1, 2)
    Row_Number = Row_Number + 1
Next i

End Sub

Output:

Running the code will collect the name of each student along with his / her marks in Physics in the range F4:G13.


Example 4 – VBA to Collect Data from Each Row of Multiple Columns of a Given Range with a Criterion

In our last example, we’ll collect data from multiple columns of a given range with a criterion.

Let’s collect the marks in Physics of all the students who achieved a mark of more than 60 along with their names (B4:C13) in a new range (F4:G13). Since the output range involves two columns (B4:C13), we’ll again use the Cells property of VBA.

The VBA code is:

VBA Code:

Sub Collecting_Data_from_Multiple_Columns_with_Criterion()

Row_Number = 1

For Each i In Range("B4:C13").Rows
    If i.Cells(1, 2) > 60 Then
        Range("F4:G13").Cells(Row_Number, 1) = i.Cells(1, 1)
        Range("F4:G13").Cells(Row_Number, 2) = i.Cells(1, 2)
        Row_Number = Row_Number + 1
    End If
Next i

End Sub

VBA Code to Use VBA for Each Row in a Range in Excel

Output:

Running the code will collect the name of each student who got more than 60 in Physics along with the corresponding marks in the range F4:G13.


Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Rifat Hassan
Rifat Hassan

Rifat Hassan, BSc, Electrical and Electronic Engineering, Bangladesh University of Engineering and Technology, has worked with the ExcelDemy project for almost 2 years. Within these 2 years, he has written over 250 articles. He has also conducted a few Boot Camp sessions on effective coding, especially Visual Basic for Applications (VBA). Currently, he is working as a Software Developer to develop and deploy additional add-ins to enhance the customers with a more sophisticated experience with Microsoft Office Suits,... Read Full Bio

2 Comments
  1. Bonjour,

    Je voudrai connaitre comment utiliser For each pour donner une hauteur Précise en fonction de la couleur de la ligne d’une plage de donnée ou d’un tableau nommé.
    Exemple ligne de couleur bleu aura une hauteur = 25
    Ligne de couler rouge aura une hauteur = 20

    Merci pour votre aide

    • Reply Avatar photo
      Rubayed Razib Suprov Dec 26, 2022 at 1:36 PM

      Hello Amghar,
      Thanks a lot for submitting Excel related problem at Exceldemy. Here you can use the VBA code mentioned below.

      Sub Detect_font_color()
      If Worksheets("Sheet1").Range("D6").Font.ColorIndex = 6 Then
      Range("D6").RowHeight = 20
      End If
      End Sub
      

      Detect Font Color and set row height to 40
      In that code, you need to mention the cell address inside the VBA code range property(as shown in the image). After this, if the condition satisfied, the cell row height will adjust accordingly.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo