Attribute VB_Name = "Examples" '========================================== ' VBA For Loop in Excel - Companion Workbook ' Article: https://excelmaster.ai/blog/vba/vba-for-loop ' Tested on Excel 365 v2509, Excel 2021, Excel 2019 ' Free to copy, modify, share. (c) ExcelMaster '========================================== Option Explicit ' ===== Example 1: Fill 10 cells with sequential numbers ===== Sub Example01_Basic() Dim i As Long For i = 1 To 10 Sheets("01_Basic").Cells(i, 1).Value = i * 10 Next i End Sub ' ===== Example 2: Use Step to fill every other row ===== Sub Example02_Step() Dim i As Long For i = 1 To 20 Step 2 Sheets("02_Step").Cells(i, 1).Value = "Row " & i Next i End Sub ' ===== Example 3: Reverse loop - safely delete rows ===== ' Why backward? Forward deletion shifts indices and causes you to skip rows. Sub Example03_Reverse() Dim i As Long Dim ws As Worksheet Set ws = Sheets("03_Reverse") For i = 10 To 1 Step -1 If ws.Cells(i, 2).Value = "DELETE" Then ws.Rows(i).Delete End If Next i End Sub ' ===== Example 4: Exit For - stop on first match ===== Sub Example04_ExitFor() Dim i As Long For i = 1 To 1000 If Sheets("04_ExitFor").Cells(i, 1).Value = "STOP" Then MsgBox "Found STOP at row " & i Exit For End If Next i End Sub ' ===== Example 5: Nested loop - fill a 6x4 grid ===== Sub Example05_Nested() Dim r As Long, c As Long For r = 1 To 6 For c = 1 To 4 Sheets("05_Nested").Cells(r, c).Value = r * c Next c Next r End Sub ' ===== Example 6: For Each over a Range ===== Sub Example06_ForEachRange() Dim cell As Range Dim total As Double For Each cell In Sheets("06_ForEachRange").Range("B2:B11") total = total + cell.Value Next cell Sheets("06_ForEachRange").Range("D2").Value = total End Sub ' ===== Example 7: For Each over an Array ===== Sub Example07_ForEachArray() Dim names As Variant names = Array("Alice", "Bob", "Carol", "Dan") Dim n As Variant Dim i As Long i = 1 For Each n In names Sheets("07_Array").Cells(i, 1).Value = n i = i + 1 Next n End Sub ' ===== Example 8: Real-world - copy rows where Amount > 200000 ===== Sub Example08_RealWorld() Dim ws As Worksheet Set ws = Sheets("08_RealWorld") Dim lastRow As Long lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ws.Range("F2:I100").ClearContents Dim outRow As Long outRow = 2 Dim i As Long For i = 2 To lastRow If ws.Cells(i, 4).Value > 200000 Then ws.Cells(outRow, 6).Value = ws.Cells(i, 1).Value ws.Cells(outRow, 7).Value = ws.Cells(i, 2).Value ws.Cells(outRow, 8).Value = ws.Cells(i, 3).Value ws.Cells(outRow, 9).Value = ws.Cells(i, 4).Value outRow = outRow + 1 End If Next i End Sub ' ===== Bonus: Run all non-destructive examples ===== Sub RunAllExamples() Example01_Basic Example02_Step Example05_Nested Example06_ForEachRange Example07_ForEachArray Example08_RealWorld MsgBox "Done. Try Example03 (run Reset03_Reverse first if needed) and Example04 individually." End Sub ' ===== Helper: Reset 03_Reverse sample data so Example03 can be re-run ===== ' Example03 deletes rows in '03_Reverse'. After running it once, the DELETE flags ' are gone. Run this Reset before re-running Example03_Reverse to restore the ' original 10-row input (Items 1..10 with rows 3/6/9 marked DELETE). Sub Reset03_Reverse() Dim ws As Worksheet Set ws = Sheets("03_Reverse") ws.Range("A1:B100").Clear Dim i As Long For i = 1 To 10 ws.Cells(i, 1).Value = "Item " & i If i = 3 Or i = 6 Or i = 9 Then ws.Cells(i, 2).Value = "DELETE" Else ws.Cells(i, 2).Value = "KEEP" End If Next i End Sub