VBA & Macro Problems

Excel VBA: Print Consecutive Cart Numbers with a Macro

John WilsonJohn Wilson Nov 12, 2026 868 views

Question details

The user needs to print multiple worksheets or print jobs with sequential cart numbers, such as 1 through 4, while calculating the required cart quantity and adjusting the final cart amount.

Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A macro-enabled workbook is used to calculate how many carts are needed, place each cart number on a worksheet, print the worksheet, and then remove the temporary values.
Observed behavior
The desired result is a series of printed sheets with consecutive cart numbers and a correctly calculated quantity for the final cart.
Before you start

Identify the cells that contain the total quantity, cart capacity, cart number, and final cart amount, then confirm that the intended printer is selected. Save the workbook as an Excel Macro-Enabled Workbook (*.xlsm) before adding or running VBA code.

Solution 1Recommended

Use an Excel VBA loop to calculate and print sequential cart numbers

Use this method when the same worksheet must be printed repeatedly with cart numbers that increase from 1 to the calculated cart total.

The example below assumes the total quantity is in B2, the cart capacity is in B3, the temporary cart number is written to B4, and the cart amount is written to B5. Replace these cell references with the locations in your worksheet.

The macro uses ceiling division to calculate the number of carts, prints each cart number, assigns the remaining quantity to the final cart, and clears the temporary cells after printing.

1
Open the VBA editor

Open the workbook in Excel, press Alt+F11, select Insert > Module, and create a standard VBA module.

2
Add the printing macro

Paste this code into the module: Sub PrintConsecutiveCarts()\n Dim ws As Worksheet\n Dim totalQty As Long, capacity As Long\n Dim cartCount As Long, cartNo As Long\n Dim cartAmount As Long\n\n Set ws = ActiveSheet\n totalQty = CLng(ws.Range("B2").Value)\n capacity = CLng(ws.Range("B3").Value)\n\n If totalQty <= 0 Or capacity <= 0 Then\n MsgBox "Enter positive values for total quantity and cart capacity.", vbExclamation\n Exit Sub\n End If\n\n cartCount = (totalQty + capacity - 1) \\ capacity\n\n For cartNo = 1 To cartCount\n If cartNo < cartCount Then\n cartAmount = capacity\n Else\n cartAmount = totalQty - capacity * (cartCount - 1)\n End If\n\n ws.Range("B4").Value = cartNo\n ws.Range("B5").Value = cartAmount\n ws.PrintOut\n Next cartNo\n\n ws.Range("B4:B5").ClearContents\n MsgBox cartCount & " cart sheet(s) printed.", vbInformation\nEnd Sub

3
Save and enable macros

In Excel, select File > Save As, choose Excel Macro-Enabled Workbook (*.xlsm), save the file, and select Enable Content when the workbook is reopened.

4
Run the macro

Return to Excel, select Developer > Macros, choose PrintConsecutiveCarts, and click Run. The worksheet will print once for each cart number, then the temporary cart cells will be cleared.

Adjust the worksheet references: Change B2, B3, B4, and B5 in the code to match your total quantity, cart capacity, cart number, and final cart amount cells. Test with a small quantity or use Print Preview before sending a large job to the printer.

Create and run a sequential cart-printing macro in WPS Spreadsheet

WPS Spreadsheet can be used to open a compatible workbook, enter the cart-printing VBA logic where macro support is available, and print sequential cart labels from the worksheet. Save the file in the appropriate Microsoft Office format and enable macros when prompted.

  1. 1. Open or create the workbook: Launch WPS Spreadsheet and open the existing .xlsm workbook, or create a worksheet containing cells for total quantity, cart capacity, cart number, and cart amount.
  2. 2. Open the macro editor: Use the WPS Spreadsheet macro or VBA editor available in your edition to insert a standard module, then add the sequential cart-printing procedure.
  3. 3. Save in a compatible format: Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) when the macro must be retained.
  4. 4. Enable and run the macro: When prompted, enable macros, then use the macro management or Developer tools to select the cart-printing procedure and run it.
Automates cart-number sequencing, quantity calculation, and repeated printingSupports familiar spreadsheet formulas, worksheets, print areas, and macro workflowsProvides compatibility with common Microsoft Excel .xlsx and macro-enabled .xlsm files where VBA support is availableOffers a lightweight alternative for managing and printing structured cart records
microsoft office alternative - wps office

Frequently Asked Questions

Why must the workbook be saved as an XLSM file?

An XLSM file stores VBA code, while a standard XLSX file does not retain macros. Use File > Save As and select Excel Macro-Enabled Workbook (*.xlsm).

How does the macro calculate the number of carts?

It uses ceiling division: (total quantity + cart capacity - 1) divided by cart capacity. This rounds up whenever the final cart contains fewer items than the normal capacity.

How is the final cart amount adjusted?

For every cart except the last, the macro assigns the full capacity. For the last cart, it subtracts the quantities already assigned from the total quantity.

Can I print only part of the worksheet?

Yes. Set a print area through Page Layout > Print Area > Set Print Area, or replace ws.PrintOut in the macro with a specific range such as ws.Range("A1:H30").PrintOut.

What should I do if the macro does not run?

Confirm that the file is saved as .xlsm, macros are enabled, the VBA code is in a standard module, and the total quantity and capacity cells contain positive numeric values.