Fix Excel VBA Macro That Must Run Twice to Copy Data Correctly
Question details
The user's Excel VBA macro fails to copy all data correctly on the first execution, requiring a second run to complete the task.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running a VBA macro script to copy data rows between different workbooks.
- Observed behavior
- Values are missing or incorrect after the first run, but they appear correctly after running the exact same macro a second time.
Before modifying your VBA code, review the end of your script to check the ScreenUpdating property and save a backup copy of your Excel workbooks to prevent accidental data loss during testing.
Correct ScreenUpdating and Qualify VBA References
Updating your VBA code to restore screen updating and explicitly stating workbooks and worksheets prevents data from pasting into the wrong active window or hiding the results.
A macro appearing to fail on the first run is almost always caused by 'Application.ScreenUpdating' being left as 'False', or by using unqualified references. Unqualified references rely on whatever sheet happens to be active, which can shift dynamically during execution.
Scroll to the very end of your macro script. If you see the line 'Application.ScreenUpdating = False', change it to 'Application.ScreenUpdating = True'. This ensures Excel refreshes the UI after the macro finishes.
Ensure every 'Range', 'Cells', and 'Rows' object in your copy/paste code is preceded by its specific worksheet object. For example, assign variables like 'Set wsSource = Workbooks("Source.xlsx").Sheets("Data")' and then strictly use 'wsSource.Range(...)'.
When determining the last row for your destination worksheet, explicitly qualify the rows count: use 'wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Row' instead of an unqualified 'Rows.Count', which might calculate based on the wrong sheet.

Verify Source Workbook States Before Copying
Ensure the VBA logic accurately detects that the source workbook is open and fully loaded before attempting to extract data from it.
Edit and Run Macros Flawlessly in WPS Spreadsheet
WPS Office provides robust support for VBA macros. You can easily open, edit, and troubleshoot your macro-enabled workbooks directly within WPS Spreadsheet, ensuring a smooth scripting experience.
- 1. Open your Macro Workbook: Launch WPS Spreadsheet and open your .xlsm or .xls file containing the macro.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon. If it is not visible, you can easily enable it in the WPS Options menu.
- 3. Open the VBA Editor: Click on the 'Visual Basic' or 'Macros' icon to launch the built-in VBA editor.
- 4. Correct the Code: Apply the explicit worksheet references and fix your ScreenUpdating logic, then save your changes.
- 5. Run and Test: Execute the macro again directly from the WPS interface to ensure data copies correctly on the very first run.

Frequently Asked Questions
Why does my macro only show results after I run it a second time?
This usually occurs when 'Application.ScreenUpdating' is left as 'False' at the end of the script. The script prevents Excel from redrawing the screen to save resources. A manual click or a second run forces the application UI to finally refresh and display the copied data.
What does an unqualified reference mean in Excel VBA?
An unqualified reference is when you use properties like 'Range' or 'Cells' without specifying which workbook or worksheet they belong to. When omitted, VBA defaults to whatever sheet is currently active. If the active sheet shifts during execution, your data is pulled from or pasted into the wrong place.
Can an incorrect last-row calculation cause a macro to skip copying data?
Yes. If your macro calculates the last row using 'Rows.Count' without specifying the target worksheet, it might pull the row count from the wrong sheet. This causes the copied data to overwrite existing rows or be placed entirely out of your viewing area, making it seem like the copy failed.




