Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA Start Loop: A Step-by-Step Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To start a loop in Excel VBA, open the VBA editor with Alt+F11, insert a module, and write a For...Next, Do...Loop, or For Each...Next block inside a Sub procedure. The loop begins when you run the macro (F5) and repeats according to the condition or counter you set. This guide walks you through writing each loop type, running it, and fixing common errors.

What You'll Need

  • Excel for Windows or Mac (the VBA editor is included; no extra install).
  • A workbook where you can safely test macros — ideally a copy, since loops can overwrite data.
  • Macro security set to allow macros (File > Options > Trust Center > Trust Center Settings > Macro Settings). On Mac, Excel > Preferences > Security. Labels vary slightly by version and region.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Fn+Alt+F11 (Mac). The editor opens in a separate window.
  2. Insert a module. In the editor, go to Insert > Module. A blank code pane appears.
  3. Create a Sub procedure. Type Sub StartLoop() and press Enter; the editor adds End Sub automatically.
  4. Choose your loop type. Use For...Next when you know how many times to repeat, Do...Loop when you repeat until a condition changes, and For Each...Next to walk through a collection such as cells in a range.
  5. Write a For...Next loop. Between Sub and End Sub, type:
    Dim i As Long
    For i = 1 To 10
        Cells(i, 1).Value = i * 2
    Next i
    This writes 2, 4, 6 … 20 into A1:A10.
  6. Write a Do...Loop. For a condition-checked loop, type:
    Dim n As Long
    n = 1
    Do While n <= 5
        Cells(n, 2).Value = n
        n = n + 1
    Loop
    The loop runs while n is 5 or less. If you forget n = n + 1, it never ends.
  7. Write a For Each...Next loop. To process every cell in a range:
    Dim c As Range
    For Each c In Range("A1:A10")
        If IsNumeric(c.Value) Then c.Value = c.Value * 2
    Next c
    This doubles numeric values and leaves text alone.
  8. Run the macro. Press F5 while the cursor is inside the Sub, or use Run > Run Sub/UserForm. To step through one line at a time, press F8.
  9. Stop the loop if needed. If a loop runs too long, press Ctrl+Break (Windows) or Command+Period (Mac) to interrupt. If that fails, close Excel; unsaved changes are lost.
  10. Save the workbook as macro-enabled. Use File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Otherwise the code is discarded.

Common Mistakes

  • Forgetting to increment the counter in a Do loop, which causes an infinite loop. Always change the variable that the condition tests.
  • Using the wrong comparison. Do While checks before running; Do Until runs until the condition becomes true. Mixing them up gives zero or too many iterations.
  • Looping over empty cells without a check, which can produce errors or overwrite blanks with zeros.
  • Hard-coding row counts instead of using Cells(Rows.Count, 1).End(xlUp).Row to find the last used row.
  • Not qualifying the worksheet. Cells(i,1) acts on the active sheet; if another sheet is active, data lands in the wrong place. Use Worksheets("Sheet1").Cells(i,1).

Tips

  • Declare loop counters as Long rather than Integer; Excel has more than 32,767 rows.
  • Turn off screen updating for large loops: Application.ScreenUpdating = False at the start and True at the end. This speeds up execution noticeably.
  • Use For Each over a range when you only read or lightly edit cells; it is often simpler and less error-prone than index-based loops.
  • Add Debug.Print i inside the loop to watch the counter in the Immediate Window (Ctrl+G).
  • Test on a copy of the workbook first. Loops that write values cannot be undone with Ctrl+Z after the macro finishes.

Troubleshooting

  • “Compile error: Expected: Do” or similar. A loop block is missing its closing Loop, Next, or Wend. Check that every opening statement has a matching closer.
  • Macro runs but nothing happens. The loop may be outside the Sub, or the condition is false on entry. Step through with F8 to see which lines execute.
  • Excel freezes. Likely an infinite loop. Press Ctrl+Break; if unresponsive, close Excel. Then check that the counter or condition variable changes inside the loop.
  • “Subscript out of range”. The sheet name in your code does not match the actual tab name. Check spelling and spacing.
  • Data appears on the wrong sheet. Qualify every Cells or Range reference with a worksheet object.
  • Macros are disabled. Enable them in Trust Center settings, or open the file from a trusted location. If your organisation blocks macros, you cannot run VBA; contact your IT administrator.

Frequently Asked Questions

How do I start a loop in Excel VBA?

Write a For...Next, Do...Loop, or For Each...Next block inside a Sub procedure in a standard module, then run the macro with F5. The loop starts at the first statement and repeats per its condition.

What is the difference between For and Do loops?

For...Next repeats a set number of times using a counter. Do...Loop repeats while or until a condition is met, which suits unknown iteration counts.

How do I stop an infinite loop?

Press Ctrl+Break (Windows) or Command+Period (Mac). If Excel does not respond, close it. Then ensure the loop’s counter or condition variable is updated inside the loop body.

Can I loop through all rows in a column?

Yes. Find the last row with Cells(Rows.Count, 1).End(xlUp).Row and use that as the upper bound in a For loop, or use For Each over the used range.

Why does my loop skip the first row?

Check your starting index. For i = 2 To 10 starts at row 2; use For i = 1 To 10 to include row 1. Also confirm the loop is not using Do Until with a condition already true at the start.

Do loops work on Excel for Mac?

Yes, VBA loops behave the same on Mac Excel, though the VBA editor and some shortcuts differ. Use Fn+Alt+F11 to open the editor and Command+Period to interrupt.