Excel VBA's If Next pattern is really two separate constructs used together: the If...Then...End If statement for conditional logic and the For...Next loop for repeating a block of code a set number of times. There is no single command called "If Next." What people mean by that phrase is a For...Next loop that contains an If test inside it — for example, looping through rows 2 to 100 and running an action only when a cell meets a condition. To achieve it, write For i = start To end, put your If condition Then block inside, close the If with End If, then close the loop with Next i. The rest of this guide shows the exact syntax, a worked example, and the mistakes that cause the macro to skip rows or throw errors.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer; on Mac, Excel > Preferences > Ribbon & Toolbar).
- A workbook you can safely test in — save a copy before running any macro that writes to cells.
- Basic familiarity with opening the VBA editor (Alt+F11 on Windows, Fn+Option+F11 on many Macs) and inserting a module (Insert > Module).
- Macro security set to allow macros in this file (File > Options > Trust Center > Trust Center Settings > Macro Settings). Labels differ slightly by Excel version and region.
Step-by-Step
- Open the VBA editor and add a module. Press Alt+F11 (Windows) or Fn+Option+F11 (Mac). In the Project pane, right-click your workbook, choose Insert > Module. A blank code window appears.
- Start the sub procedure. Type
Sub IfNextExample()and press Enter. VBA adds the closingEnd Subautomatically. - Declare your variables. Add
Dim i As Longfor the loop counter andDim lastRow As Longfor the end of the range. UsingLongrather thanIntegeravoids overflow on large sheets. - Find the last used row. Write
lastRow = Cells(Rows.Count, 1).End(xlUp).Row. This counts up from the bottom of column A so the loop stops at real data instead of row 1,048,576. - Open the For...Next loop. Type
For i = 2 To lastRow. Starting at 2 skips a header row in row 1. The loop will run once for every value ofifrom 2 tolastRow. - Add the If test inside the loop. On the next line type
If Cells(i, 1).Value > 100 Then. This checks column A on the current row. Press Enter; VBA indents the line to show it is inside the loop. - Put the action inside the If. Type
Cells(i, 2).Value = "High". This writes "High" into column B only when the condition is true. - Close the If and the loop. Type
End Ifon its own line, thenNext i. The order matters:End Ifcloses the conditional,Next icloses the loop. VBA will refuse to compile if they are reversed. - Add an optional Else branch. If you want a different result when the test fails, insert
Elsebetween the action andEnd If, then add the alternative line, for exampleCells(i, 2).Value = "Low". - Run the macro. Press F5 with the cursor inside the procedure, or use Run > Run Sub/UserForm. Switch back to the sheet to confirm column B now shows the results row by row.
- Step through to verify. Press F8 to execute one line at a time. The yellow highlight shows exactly which row the loop is on, which is the fastest way to spot an off-by-one error.
Common Mistakes
- Forgetting
End If. A multi-line If withoutEnd Ifproduces a compile error. Single-line Ifs (If x Then y) do not need it, but mixing the two styles causes confusion. - Putting
NextbeforeEnd If. The loop must enclose the whole If block. IfNext iappears inside the If, the loop only advances when the condition is true and can run forever. - Hard-coding the end row.
For i = 2 To 100silently ignores data below row 100. CalculatelastRowinstead. - Using
ActiveCellorSelectioninside the loop. The loop counterishould drive the cell reference, e.g.Cells(i, 1), or the macro will act on the same cell repeatedly. - Deleting rows while looping forward. When you delete a row, the rows below shift up and the loop skips the next one. Loop backwards with
For i = lastRow To 2 Step -1when deleting. - Comparing text to numbers.
Cells(i,1).Value > 100treats text as greater than any number in VBA. UseIsNumericfirst if the column can contain text.
Tips
- Turn on
Option Explicitat the top of the module so undeclared variables raise an error instead of silently defaulting to Empty. - Use
ElseIffor multiple conditions rather than nesting many separate If blocks; it is easier to read and faster to debug. - For very large ranges, read the data into a Variant array first, loop the array in memory, then write back in one go. Cell-by-cell writes are slow on tens of thousands of rows.
- Name the loop counter something meaningful (
rowNuminstead ofi) in longer procedures. - Add
Application.ScreenUpdating = Falseat the start and set it back toTrueat the end to speed up macros that touch many cells.
Troubleshooting
Compile error: Expected: End If or Next. The If and Next are mismatched. Count your openings and closings: every multi-line If needs one End If, and every For needs one Next. Indent consistently so the nesting is visible.
The macro runs but nothing changes. The condition is probably never true. Check the data type in the tested column — numbers stored as text will not compare as expected. Temporarily add Debug.Print Cells(i,1).Value inside the loop and read the Immediate window (Ctrl+G) to see what is actually being evaluated.
Run-time error 1004. Usually an invalid cell reference, often because lastRow returned 1 on an empty sheet and the loop tried to read row 2 of nothing. Guard with If lastRow < 2 Then Exit Sub.
The loop never ends. You likely modified i inside the loop body, or placed Next inside the If. Press Ctrl+Break to stop execution and restructure the block.
Results land in the wrong column. Cells(i, 1) is column A, Cells(i, 2) is column B. The second argument is the column number, not the row.
Frequently Asked Questions
Is there an actual "If Next" statement in VBA?
No. VBA has If...Then...End If and For...Next as separate statements. "If Next" is shorthand for combining them.
Can I use If without End If inside a For loop?
Only for single-line Ifs such as If x > 0 Then y = 1. Any If spanning multiple lines needs End If.
What is the difference between For Next and For Each?
For...Next counts through numbers; For Each...Next walks through a collection such as all cells in a range. Both can contain If tests.
How do I skip to the next iteration?
Use If condition Then GoTo SkipLine with a label, or better, restructure with Else. VBA has no Continue keyword.
Can I nest a For loop inside an If?
Yes, and an If inside a For is equally valid. Each Next must match its own For, and each End If its own If.
Why does my loop skip every other row?
You are almost certainly deleting or inserting rows while looping forward. Loop backwards with Step -1 when the row count changes.