Fix Excel VBA Saving Only the First Invoice Item
Question details
An Excel VBA macro intended to copy invoice data to an order log is failing to capture all line items, transferring only the first row.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Running a VBA script to process an invoice and write its detail lines to an ongoing OrderLog worksheet.
- Observed behavior
- The macro successfully runs but only saves the first invoice detail line to the order log, ignoring all subsequent items on the invoice.
Before modifying your VBA script, ensure you have saved a backup copy of your Excel workbook (.xlsm) to prevent any accidental data loss or macro corruption during testing.
Correct the VBA Loop Array Assignment
Update your macro script to properly iterate through all invoice lines, ensuring both line-level details and invoice-level headers are assigned inside the loop.
This issue typically occurs because header values or array indices are mistakenly hardcoded to row 1, or the destination row counter is not placed inside the detail loop.
To optimize the process and resolve the issue, the data should be read into an array, manipulated in memory, and then printed out to the OrderLog sheet.
Press Alt + F11 on your keyboard to open the Visual Basic Editor and locate the module containing your invoice-saving routine.
Load the entire invoice detail range into a memory array. This allows the script to process the data much faster than reading it cell by cell.
Create a loop (e.g., For i = 1 to UBound(SourceArray)) to iterate through each row. Add a condition to skip any empty detail rows.
Inside your loop, increment the destination row counter (e.g., n = n + 1) for every valid invoice item found.
Assign both the invoice-level values (like Date and Invoice Number) and the line-item details to your destination array using the dynamic variable counter, such as br(n, column).
After the loop finishes, write the fully populated array to the OrderLog sheet in one operation to complete the macro execution.

Try WPS Office for Seamless Macro and VBA Support
If you frequently work with complex spreadsheets and macros, WPS Office provides a highly compatible, fast, and feature-rich environment. Enjoy full support for .xlsm files and VBA scripting without the heavy subscription costs.
- 1. Download and Install WPS Office: Visit the official WPS website to download the lightweight installer and set up the suite on your computer.
- 2. Open Your Macro Workbook: Launch WPS Spreadsheets and open your .xlsm file. Your formatting and scripts will be retained perfectly.
- 3. Enable Macros and Run: Navigate to the Developer tab, enable macros, and run your VBA routines just as you would in Microsoft Excel.

Frequently Asked Questions
Why does my VBA macro overwrite the first row repeatedly?
This happens when the row counter variable (e.g., 'n') is not properly incremented inside the loop. If the counter does not increase by 1 for each line item, the script will keep writing new data to the same array index or worksheet row.
How do I find the last used row in an Excel sheet using VBA?
You can dynamically find the last used row in a specific column using a command like: LastRow = Cells(Rows.Count, "A").End(xlUp).Row. This prevents the script from looping through thousands of empty cells.
Is it faster to use memory arrays instead of writing directly to cells?
Yes. Reading a worksheet range into a VBA array, processing it in memory, and dumping the final array back to the worksheet is significantly faster and less resource-intensive than iterating over and writing to individual cells one by one.




