How to Use VBA to Loop Through Rows in Range in Excel (6 Examples)

 

 

Method 1 – Using Excel VBA Macro with Range Variable to Loop Through Rows

Use VBA in Range with Range Variable to Loop Through Rows

STEPS:

  • Go to the active worksheet ‘Range Variable’.
  • Right-click and select the option ‘View Code’. You can also press Alt + F11 to open it.

Use VBA in Range with Range Variable to Loop Through Rows

  • A code window for that worksheet will open.
  • Enter the code in the code window:
Sub VBA_Loop_through_Rows()
Dim w As Range
For Each w In Range("B5:D9").Rows
w.Cells(1).Interior.ColorIndex = 35
Next
End Sub
  • Click on Run or press F5 to run the code.

Use VBA in Range with Range Variable to Loop Through Rows

We will get results like the following screenshot.

Read More: Excel VBA: Loop Through Columns in Range


Method 2 – Applying VBA to Loop Through Rows with Numeric Variable

Apply VBA to Loop Through Rows in Range with Numeric Variable

STEPS:

  • Right-click on the active sheet named ‘Numeric Value’.
  • Select the option ‘View Code’.

Apply VBA to Loop Through Rows in Range with Numeric Variable

  • A code window for that worksheet will open. Press Alt + F11 to open that code window.
  • Enter the following code in the window:
Sub VBA_Numeric_Variable()
Dim w As Integer
With Range("B5").CurrentRegion
For w = 1 To .Columns.Count
.Columns(w).NumberFormat = "$0.00"
Next
End With
End Sub
  • Click Run or press the F5 key to run the code.

We can see the results in the following picture. The above code changes the format of the number into decimal points.

Read More: VBA to Loop through Rows and Columns in a Range in Excel


Method 3 – Applying Macro with User-Selected Range to Loop Through Rows

Excel VBA in User-Selected Range to Loop Through Rows in Range

STEPS:

  • Select the cell range (D5:D9).

Excel VBA in User-Selected Range to Loop Through Rows in Range

  • Right-click on the active sheet named ‘User Selected’. Select the option ‘View Code’.

Excel VBA in User-Selected Range to Loop Through Rows in Range

  • A VBA code window for the active worksheet will open. You can also open that code window by pressing Alt + F11.
  • Enter the following code in the code window:
Sub VBA_User_Selection()
Dim w As Variant
Set xRange = Selection
For Each w In xRange
MsgBox "Cell value = " & w.Value
Next w
End Sub
  • To run the code for that worksheet, click Run or press the F5 key.

Excel VBA in User-Selected Range to Loop Through Rows in Range

  • A message box appears showing the first value of the selected range.

  • Click on OK to return the second value of the selected range, cell D6.

  • Continue this process until the selected range’s last value, cell D9.

Read More: How to Use VBA to Count Rows in Range with Data in Excel


Method 4 – Performing VBA Loop Through Rows in Dynamic Range

Loop Through Rows in Dynamic Range with VBA

STEPS:

  • Input value 6 in cell B1 and C in cell B2.
  • Right-click on the active cell and select the option ‘View Code’.

Loop Through Rows in Dynamic Range with VBA

  • A VBA code window for the active worksheet will open. Or press Alt + F11.
  • Enter the following code in that code window:
Sub Dynamic_Range()
Dim xRange As String
xRange = "B8:" + Worksheets("Dynamic Range").Cells(2, 2).Value + _
CStr(3 + Worksheets("Dynamic Range").Cells(1, 2).Value)
For Each Row In Range(xRange)
For Each Cell In Row
Cell.Value = "$2500.00"
Next Cell
Next Row
End Sub
  •  Click Run or press the F5 key to run the code.

Loop Through Rows in Dynamic Range with VBA

  • The dataset fills with the value of $2500.00.

NOTE: In the above image, the value 6 represents the first two rows of the range (B8:B9).

  • Input the value 9 in cell B1 instead of 6.

The results are shown in the following image.


Method 5 – Inserting a Loop Through an Entire Row in Excel Range

Insert VBA to Loop Through Entire Row in Range

STEPS:

  • Right-click on the active sheet named ‘Entire Row’. Select the option ‘View Code’.

Insert VBA to Loop Through Entire Row in Range

  • A blank VBA code window for the active worksheet will open. Or press Alt + F11.
  • Enter the following code in that code window:
Sub VBA_Loop_Entire_Row()
Dim w As Range
For Each w In Range("5:9")
If w.Value = "Chris" Then
MsgBox "Chris found at " & w.Address
End If
Next w
End Sub
  • Click Run or press the F5 key to run the code.

Insert VBA to Loop Through Entire Row in Range

  • In the above image, the highlighted value ‘Chris’ indicates the value we will search for. The range value ‘5:9’ indicates we will search for the value in the cell range (B5:B9).
  • A message box displays the value ‘Chris’ in cell B6.

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


Method 6 – Using Macro with Loop Through Every n-th Row in Range

Loop Through Every n-th Row in Range with Excel VBA

STEPS:

  • Right-click on the active sheet named ‘n-th Row’. Select the option ‘View Code’.

Loop Through Every n-th Row in Range with Excel VBA

  • A blank VBA code window for that worksheet will open. Or press Alt + F11.
  • Enter the following code in that code window:
Sub ShadeRows1()
Dim r As Long
With Range("B5").CurrentRegion
For r = 1 To .Rows.Count
If r / 2 = Int(r / 2) Then 'even rows
.Rows(r).Interior.ColorIndex = 43
End If
Next
End With
End Sub
  • Click Run or press the F5 key to run the code.

  • The above code shades only the odd number rows from our dataset.

Read More: Excel VBA to Loop through Range until Empty Cell


Download the Practice Workbook

We can download the practice workbook from here.


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mukesh Dipto
Mukesh Dipto

Mukesh Dipto is an excellent marine engineer who loves working with Excel and diving into VBA programming. For him, programming is like a superhero tool that saves time when dealing with data, files, and the internet. His skills go beyond the basics, including Rhino3D, Maxsurf C++, AutoCAD, HTML, CSS, JavaScript, and WordPress. He got his B.Sc in Naval Architecture & Marine Engineering from BUET, and now he's switched gears, working as a content developer. In this role, he... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo