VBA & Macro Problems

Create Excel Invoices and Track Inventory Automatically

Nimra MalikNimra Malik Nov 18, 2026 872 views

Question details

Create an invoice system that retrieves product prices and updates inventory quantities.

Create Invoices and Track Inventory in Excel
Product
Microsoft Excel
Device & OS
not provided
Scenario
Use an inventory sheet to prepare invoices and maintain an auditable record of sales.
Observed behavior
Invoices should calculate prices and totals, while recorded sales reduce available stock.
Before you start

Prepare a unique product ID, price, and opening stock quantity for each item. Create three worksheets named Inventory, Invoice, and Sales.

Solution 1Recommended

Use an invoice template and sales ledger to update stock

Use formulas to populate an invoice and recalculate stock when finalized invoice lines are added to a sales ledger.

This approach updates stock automatically after sales are recorded, but posting invoice lines to the ledger is a manual step. Fully automatic posting requires a separately designed and tested macro or other automation.

1
Set up inventory

On Inventory, enter the headers Product ID, Description, Price, Opening Stock, and Current Stock in cells A1:E1. Add one product per row. In E2, enter =D2-SUMIF(Sales!$B:$B,A2,Sales!$C:$C) and fill it down.

2
Create the sales ledger

On Sales, enter Invoice ID, Product ID, Quantity, and Date in cells A1:D1. Keep each finalized invoice line on a separate row.

3
Build the invoice template

On Invoice, use B1 for the invoice ID and enter Product ID, Quantity, Unit Price, and Line Total in A4:D4. In C5, enter =IF(A5="","",IFERROR(VLOOKUP(A5,Inventory!$A$2:$D$1000,3,FALSE),"")) and fill down through C14.

4
Calculate invoice totals

In Invoice!D5, enter =IF(OR(A5="",B5="",C5=""),"",B5*C5) and fill down through D14. Enter =SUM(D5:D14) in D15, then enter product IDs and quantities in columns A and B.

5
Record the finalized invoice

Check that each product has a price, that the combined quantity for each product does not exceed current stock, and that the invoice ID is not already in Sales column A. Copy the filled product ID and quantity cells from Invoice columns A:B to the next empty rows in Sales columns B:C using Paste Special > Values. Enter the invoice ID and date in columns A and D for every new line.

Use an invoice template and sales ledger to update stock
Prevent duplicate postings: Check the Sales ledger before posting an invoice. Adding the same invoice twice will reduce the calculated stock twice.
Create invoices and track stock

Build an invoice and inventory workbook in WPS Spreadsheet

Use WPS Spreadsheet formulas to retrieve prices, calculate invoice totals, and update stock from a sales ledger. Posting finalized invoice lines to the ledger remains a manual step.

  1. 1. Create the worksheets: Open WPS Spreadsheet, create a workbook, and name three sheets Inventory, Invoice, and Sales.
  2. 2. Enter inventory and ledger headers: On Inventory, add Product ID, Description, Price, Opening Stock, and Current Stock in columns A:E. On Sales, add Invoice ID, Product ID, Quantity, and Date in columns A:D.
  3. 3. Add the stock formula: Enter =D2-SUMIF(Sales!$B:$B,A2,Sales!$C:$C) in Inventory!E2 and fill it down for your products.
  4. 4. Prepare the invoice: On Invoice, use B1 for the invoice ID and columns A:D for Product ID, Quantity, Unit Price, and Line Total. Enter =IF(A5="","",IFERROR(VLOOKUP(A5,Inventory!$A$2:$D$1000,3,FALSE),"")) in C5 and =IF(OR(A5="",B5="",C5=""),"",B5*C5) in D5. Fill both formulas down and use =SUM(D5:D14) for the total.
  5. 5. Post the sale: After checking prices, available quantities, and the invoice ID, use Paste Special > Values to copy the filled product IDs and quantities to Sales columns B:C. Add the invoice ID and date to each new row; Inventory will recalculate the stock.
Look up product prices and calculate invoice totals with formulasTrack remaining stock from a separate sales ledgerOpen and save Microsoft Excel XLSX workbooksUse a familiar spreadsheet interface for free
QA img-9

Frequently Asked Questions

Why is a product price blank on the invoice?

Check that the product ID in Invoice column A exactly matches an ID in Inventory column A and that the product is within the lookup range A2:D1000. The IFERROR formula displays a blank when it cannot find a match.

Does creating an invoice automatically deduct stock?

Not with the formula-only setup. Stock recalculates when the finalized invoice lines are added to the Sales ledger. Fully automatic posting requires additional automation, such as a tested VBA macro.

How can I avoid deducting stock twice?

Give each invoice a unique ID and check Sales column A before posting it. Record each finalized invoice only once.

What happens if an invoice contains the same product on multiple lines?

The stock formula subtracts all recorded quantities for that product. Before posting, add the quantities across those invoice lines and compare the combined amount with current stock.