VBA & Macro Problems

Save Invoice Line Items to Another Excel Sheet with VBA

Adam DavisAdam Davis Nov 18, 2026 868 views

Question details

Save each invoice line item as a separate row on another worksheet.

Save Invoice Line Items to Another Excel Sheet with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
An invoice worksheet contains shared invoice details and multiple line items that need to be appended to a Results worksheet.
Observed behavior
Each output row should contain the shared invoice details, one line item's values, and the invoice total.
Before you start

Create worksheets named Invoice and Results, and unmerge the cells in Invoice!A1:E25. The example assumes invoice details are in B2, C2, and E2, line items are in rows 8–24, and the total is in E25; adjust the code if your layout differs.

Solution 1Recommended

Append invoice line items with an array-based VBA macro

Use this macro to collect invoice data in memory and write all line-item rows to Results in one operation.

In this example, a nonblank cell in column A marks a line item. Each output row contains B2, C2, E2, the item's values from columns B–E, and E25, in that order.

The macro finds the append position using column A on Results, so keep that column populated for existing records. Ensure the item marker cells in Invoice!A8:A24 do not contain error values.

1
Open the VBA editor

In Excel, press Alt+F11, then select Insert > Module.

2
Add the macro

Paste this code into the module: Sub SaveInvoiceItems() Dim src As Variant, output() As Variant Dim i As Long, j As Long, n As Long, itemCount As Long Dim nextRow As Long Dim ws As Worksheet src = ThisWorkbook.Worksheets("Invoice").Range("A1:E25").Value For i = 8 To 24 If Trim(CStr(src(i, 1))) <> "" Then itemCount = itemCount + 1 Next i If itemCount = 0 Then MsgBox "No invoice line items found." Exit Sub End If ReDim output(1 To itemCount, 1 To 8) For i = 8 To 24 If Trim(CStr(src(i, 1))) <> "" Then n = n + 1 output(n, 1) = src(2, 2) output(n, 2) = src(2, 3) output(n, 3) = src(2, 5) For j = 2 To 5 output(n, j + 2) = src(i, j) Next j output(n, 8) = src(25, 5) End If Next i Set ws = ThisWorkbook.Worksheets("Results") If Application.WorksheetFunction.CountA(ws.Columns(1)) = 0 Then nextRow = 1 Else nextRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1 End If ws.Cells(nextRow, 1).Resize(itemCount, 8).Value = output End Sub

3
Save and run

Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). In Excel, open Developer > Macros, select SaveInvoiceItems, and click Run.

4
Check the results

Open the Results sheet and verify that each line item appears on its own row with the expected invoice details and total.

Append invoice line items with an array-based VBA macro
Repeated runs append duplicates: Running the macro again for the same invoice adds the same line items again; it does not check for duplicates.
Work with invoice macros in WPS

Save invoice line items with WPS Spreadsheets

In a WPS Spreadsheets installation that supports VBA macros, use the same array-based approach to append invoice line items to a Results sheet. VBA availability depends on your WPS version and installation.

  1. 1. Check macro support: Open the workbook in WPS Spreadsheets and check the Developer tab for Visual Basic or macro tools. VBA support may require a compatible version or additional components.
  2. 2. Prepare the sheets: Create Invoice and Results sheets, unmerge Invoice!A1:E25, and check that the cells match the layout described above.
  3. 3. Insert the macro: Choose Developer > Visual Basic, insert a module, and paste the SaveInvoiceItems macro from the solution above.
  4. 4. Run and verify: Save the file as XLSM, run SaveInvoiceItems from Developer > Macros, and check the appended rows on Results.
Append multiple invoice line items in a single writeWork with Microsoft Excel XLSX and XLSM formatsUse a familiar spreadsheet interface for invoice data
microsoft office alternative - wps office

Frequently Asked Questions

Why should I unmerge cells on the invoice sheet?

Merged cells can make the data layout inconsistent: only the top-left cell of a merged range holds its value. Select the source range and use Home > Merge & Center > Unmerge Cells before checking the cell references.

How do I change the invoice line-item range?

Update both For i = 8 To 24 loops to match your item rows. If the rows extend beyond 25, also expand the A1:E25 source range and update the total reference if needed.

What happens if the Results sheet already has data?

The macro finds the last populated cell in Results column A and writes the new rows below it. Keep column A populated for existing records so the append position is correct.

Why can't I run the macro after saving the workbook?

Save the file as an XLSM workbook using File > Save As, and check whether macros are enabled for the file. In WPS Spreadsheets, also verify that your version and installation support VBA.