Excel VBA For Loop Backwards: Syntax and Examples

VBA & macros · Updated November 21, 2026

Yes. To run an Excel VBA For loop backwards, use For counter = start To finish Step -1; for example, For i = 10 To 1 Step -1 counts from 10 down to 1. The negative step is essential, and reverse counting is especially useful when removing worksheet rows or items from an indexed collection.

What a Backwards For Loop Does

A standard VBA For...Next loop assigns a starting value to a counter, runs the code inside the loop, changes the counter by the step amount, and repeats until it passes the ending value. If you omit Step, VBA uses a step of 1, which moves the counter upward. Adding Step -1 makes it move downward instead.

The starting and ending values are included when the loop reaches them. Therefore, For i = 5 To 1 Step -1 visits 5, 4, 3, 2, and 1. By contrast, For i = 1 To 5 Step -1 does not count upward: the starting value is already beyond the ending value in the direction of travel, so the loop body does not run. To count upward, use a positive step or omit Step.

Reverse loops are not a separate VBA feature or a special Excel setting. They are ordinary For...Next loops with a negative step. You can use the same pattern with worksheet row numbers, array indexes, collection positions, or any other numeric sequence.

Basic VBA Syntax and Example

The general pattern is For counter = highestValue To lowestValue Step -1, followed by the statements to repeat and Next counter. Choose a counter variable such as i or rowNum, and use a numeric type such as Long for row numbers and indexes. A Long can hold Excel worksheet row numbers; an Integer cannot hold all of them.

This small macro displays the numbers in descending order in the Immediate window:

Sub CountDownExample()
    Dim i As Long

    ' Print the numbers from 10 down to 1.
    For i = 10 To 1 Step -1
        Debug.Print i
    Next i
End Sub

In the Visual Basic Editor, open the Immediate window with Ctrl+G on Windows to see the output. On Mac, use the editor’s View menu to show the Immediate window; available shortcuts can differ by keyboard and Excel setup. The loop itself is identical on Windows and Mac. To open the editor, Windows users commonly use Alt+F11; on Mac, use Developer > Visual Basic, or the corresponding Visual Basic Editor command if the Developer tab is not shown.

In a loop with more than one statement, each statement between For and Next runs once for every counter value. Indenting those statements makes the loop boundaries easier to read. Naming the counter after what it represents, such as rowNum, can also make worksheet code clearer than using a generic letter.

Looping Backwards Through Worksheet Rows

One of the most useful cases is deleting rows that meet a condition. When you delete a row while moving down the sheet, the rows below it shift up and their row numbers change. A loop that then advances may skip the row that moved into the position just checked. Starting at the bottom and moving upward avoids that shifting problem: deleting a row does not change the row numbers of the unvisited rows above it.

For example, this macro checks column A from the last populated cell up to row 2 and deletes rows where column A is blank. It assumes row 1 contains a header and that column A is the column used to determine the last row:

Sub DeleteBlankRowsInColumnA()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowNum As Long

    ' Set the sheet and find its last used row in column A.
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    ' Work upward so deleting a row does not skip the next one.
    For rowNum = lastRow To 2 Step -1
        If Len(ws.Cells(rowNum, "A").Value) = 0 Then
            ws.Rows(rowNum).Delete
        End If
    Next rowNum
End Sub

Before running a macro that deletes or changes data, save a separate copy of the workbook. VBA macros cannot be undone with Ctrl+Z, so do not rely on Excel’s Undo command to restore deleted rows. Also replace Sheet1 with the actual worksheet name. If column A can contain gaps below the true last row of data, use a suitable key column or another method to determine the range you intend to inspect.

If the macro should examine every row in a known range, set the start row explicitly rather than relying on the last used cell. For example, changing the loop header to For rowNum = 500 To 2 Step -1 checks that fixed span. Make sure the starting row is not above the ending row and that the range matches the sheet layout.

Using Reverse Loops with Arrays and Collections

Arrays and collections use indexes, so the same descending pattern applies. VBA arrays can have different lower bounds, depending on how they were created or declared. Using LBound and UBound instead of assuming that the first index is zero or one makes an array loop more adaptable.

Sub PrintArrayBackwards()
    Dim items As Variant
    Dim i As Long

    ' Array creates a zero-based array in this example.
    items = Array("North", "East", "South", "West")

    ' Visit each valid index from the last down to the first.
    For i = UBound(items) To LBound(items) Step -1
        Debug.Print items(i)
    Next i
End Sub

This example prints West, South, East, and North. For a dynamic array that has not been allocated, calling LBound or UBound can raise an error, so confirm that the array contains bounds before looping over it.

A VBA Collection is normally indexed starting at 1. If you remove items by index, looping from collection.Count down to 1 is often appropriate because removing a later item does not change the indexes of earlier items. A For Each loop is convenient for reading every item, but it does not provide a reverse direction; use an indexed loop when the order or removal behavior matters.

When to Choose a Backwards Loop

Use a descending loop when the task depends on processing later positions before earlier ones. The most common situations include:

  • Deleting worksheet rows: work from the bottom toward the top so remaining row numbers above a deletion stay stable.
  • Removing indexed collection items: deleting a higher index leaves lower indexes unchanged.
  • Reading items in reverse order: process the newest or last-positioned item first when that order is meaningful.
  • Working through an array from its final element: use the upper and lower bounds to set the loop endpoints.

A reverse loop is not automatically safer for every task. If you are only reading worksheet values and do not change the sheet structure, a normal top-to-bottom loop may be just as clear. When order does not matter and the data can be handled as a range operation, a built-in Excel feature or a bulk read/write approach may be simpler and faster than inspecting every cell individually.

Common Errors and How to Avoid Them

The most frequent mistake is writing a descending range without a negative step, such as For i = 10 To 1. VBA assumes a positive step of 1, so the loop does not execute because the starting value is already above the ending value. Add Step -1 to make the direction explicit.

Another common problem is reversing the endpoints. For a descending loop, the first value should be greater than or equal to the final value. Check the range before running the macro, especially when the endpoints are variables. A last-row calculation can return 1 when a column contains only a header or is empty, so a loop from lastRow to 2 will then run zero times. That may be intended, but it is worth considering when diagnosing a macro that appears to do nothing.

Also check the following details:

  • Use the right counter type. Use Long for worksheet rows and large indexes.
  • Keep the step negative. A step of zero does not move the counter and can produce a loop that never finishes.
  • Use the correct bounds. Arrays may start at 0 or 1, and a collection usually starts at 1.
  • Use a different counter in nested loops. Reusing and changing the same variable in an inner loop can disrupt the outer loop.
  • Do not change the counter unexpectedly inside the loop. Let the For...Next structure control its progression unless there is a specific, carefully handled reason not to.

If a loop unexpectedly skips work, add a temporary Debug.Print rowNum inside it or step through the macro in the Visual Basic Editor. Confirm the values and direction before adding destructive actions such as deleting rows.

Reverse Loops, Performance, and Other Options

A negative step changes only the order in which the loop visits values; it does not by itself make code faster. The work inside the loop usually matters more. Repeatedly reading or writing worksheet cells one at a time can take longer than processing values in memory, particularly for a large range. For a performance-sensitive macro, a common approach is to read a range into a Variant array, inspect or transform the array, and write results back in a batch. Whether that is worthwhile depends on the size of the data and what the macro needs to do.

For deleting rows based on a condition, a backwards loop is easy to understand and reliable for modest workloads. For large datasets, filtering the data and deleting matching rows as a group may be an alternative, but it requires careful handling of headers, filters, and the exact rows to remove. Do not replace a clear loop with a more complex technique unless it fits the workbook and has been checked on a copy.

For stepping down by a larger interval, VBA accepts other negative steps. For instance, a loop from 20 to 2 with Step -2 visits 20, 18, 16, and so on through 2. The step must move toward the ending value, and the endpoint is visited only if the sequence lands on it.

Frequently Asked Questions

What is the syntax for a backwards For loop in Excel VBA?
Use For counter = startValue To endValue Step -1, put the repeated code before Next counter, and make the starting value greater than or equal to the ending value.

Can I use Step -1 to delete worksheet rows?
Yes. Start at the last row you want to check and count down to the first. Deleting a row then does not shift the row numbers of the unprocessed rows above it. Save a copy first because VBA changes cannot be undone with Ctrl+Z.

Why does my VBA loop not run when counting down?
Check that you included Step -1 and that the starting value is not less than the ending value. A loop from 1 to 10 with a negative step has no values to visit in that direction.

Can a For Each loop run backwards?
Not directly: For Each does not specify an index direction. Use a numeric For loop with descending indexes when reverse order is required, or when you need to remove items by their positions.

Does a backwards loop work with arrays?
Yes. Set the first loop value to UBound(arrayName) and the final value to LBound(arrayName), then use Step -1. Confirm the array is allocated before requesting its bounds.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.