VBA & Macro Problems

Fix an Excel VBA For-Next Loop When Copying Data

Bushra ParveenBushra Parveen Dec 2, 2026 868 views

Question details

Correct a VBA loop that copies matching records between Excel worksheets.

Fix an Excel VBA For-Next Loop When Copying Data
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro scans source rows and copies matching records to another worksheet.
Observed behavior
The copied data appears briefly and then disappears, while the loop counter is modified inside the For-Next loop.
Before you start

Open the workbook and locate the macro that copies the records. Check which source rows should be processed and how the variable last is calculated.

Solution 1Recommended

Use a Step value instead of changing the loop counter

Use Step 3 when the macro should process every third row, starting at row 4.

1
Open the macro

In Excel, open Developer > Visual Basic, then find the procedure that copies the matching records.

2
Change the loop declaration

Replace For j = 4 To last with For j = 4 To last Step 3. Keep the existing matching and copying statements inside the loop.

3
Remove the extra increment

Delete the j = j + 2 statement inside the loop, and leave Next j in place. The For-Next loop will advance j by three on each iteration.

4
Test the row pattern

Run the macro or use Debug > Step Into in the Visual Basic editor. Confirm that j visits rows 4, 7, 10, and so on, up to last, and check the destination worksheet.

Use a Step value instead of changing the loop counter
If data still disappears: Check the remaining code for statements that clear or overwrite the destination range. Changing the loop increment alone may not resolve those issues.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative for viewing and editing spreadsheets in a familiar interface. Open compatible Excel files to move routine spreadsheet work to WPS; fixing an Excel VBA macro still requires checking the macro in Excel.

Open and edit Microsoft Excel XLSX and XLS files.Review copied worksheet data in WPS Spreadsheets.Use a familiar spreadsheet interface for everyday tasks.Move compatible spreadsheet files between Excel and WPS Office.
microsoft office alternative - wps office

Frequently Asked Questions

Why should I avoid changing j inside a For-Next loop?

For-Next controls the counter using the loop's Step value. Changing j inside the loop makes the rows visited harder to predict and can cause records to be skipped.

Which rows does For j = 4 To last Step 3 process?

It processes rows 4, 7, 10, and so on, stopping before j exceeds last. Check the calculation of last in the macro to confirm the intended range.

What if I need to process every row instead?

Use For j = 4 To last without Step 3 and remove the separate j = j + 2 statement. The default step is 1.

What should I check if copied data still disappears?

In the Visual Basic editor, use Debug > Step Into and inspect the destination worksheet as the macro runs. Look for later statements that clear the range or write new data over it.