How to Calculate Remaining Cartons and Bags with an Excel Formula
Question details
The user needs an Excel formula to accurately calculate remaining inventory stored in cartons and bags after individual bags are consumed.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Tracking a two-tiered inventory system (e.g., cartons and loose bags) where consuming smaller units (bags) must dynamically reduce the larger unit (carton) count and display the correct remaining balance.
- Observed behavior
- A dynamic formula is required to automatically convert total remaining stock back into correct carton and bag quantities based on a specific conversion rate (e.g., 20 bags per carton).
Ensure your inventory data is organized into clear columns for starting cartons, starting bags, used bags, and the specific number of bags per carton before applying the formula.
Use LET and HSTACK Functions for Dynamic Inventory Calculation
This method uses advanced dynamic array functions to simultaneously calculate and output both remaining cartons and remaining bags in adjacent cells.
By utilizing the LET function, you can define the total remaining bags once to keep the formula clean. The HSTACK function then arrays the results for cartons and bags horizontally.
The formula calculates the total initial bags, subtracts the used amount, and converts the remainder back into cartons and bags.
Ensure your data is placed in specific cells: A2 for Starting Cartons, B2 for Starting Bags, C2 for Bags Used, and G2 for Bags per Carton (e.g., 20).
Click on the cell where you want the remaining cartons to appear (for example, D2). Keep the cell to its immediate right (E2) empty to allow the formula to spill the remaining bags.
Type the following formula into the formula bar: =LET(Remaining, G2*A2+B2-C2, HSTACK(QUOTIENT(Remaining-1, G2), MOD(Remaining-1, G2)+1))
Press Enter. The cell D2 will display the remaining cartons, and the adjacent cell E2 will automatically display the remaining bags.

Calculate Cartons and Bags Separately (For Older Versions)
If your spreadsheet software does not support LET or HSTACK, use separate mathematical formulas for cartons and bags.
Manage Your Inventory Effortlessly with WPS Spreadsheet
WPS Spreadsheet fully supports dynamic arrays and advanced formulas like LET and HSTACK, making complex two-tiered inventory calculations simple and highly efficient.
- 1. Open WPS Spreadsheet: Launch WPS Office and create a new inventory tracking workbook.
- 2. Organize your data: Input your inventory data into designated columns: Initial Cartons, Initial Bags, Used Bags, and Bags per Carton.
- 3. Apply the formula: Paste the provided LET and HSTACK formula into the target cell where you want to calculate the remaining stock.
- 4. View automated results: Press Enter to instantly view your updated carton and bag counts in adjacent columns.

Frequently Asked Questions
Why am I getting a #NAME? error when using the LET function?
The #NAME? error occurs if your version of Excel or spreadsheet software does not support the LET or HSTACK functions, which are available in newer versions. If you are using an older version, use the separate INT and MOD formulas provided in the alternative solution.
What does the MOD function do in this inventory formula?
The MOD function calculates the remainder after a division. In this inventory context, it determines exactly how many loose bags remain after dividing the total remaining bags by the number of bags in a full carton.
How can I prevent a #SPILL! error when using HSTACK?
A #SPILL! error happens when there is existing data blocking the formula from outputting its results into adjacent cells. Ensure the cell immediately to the right of your formula (where the remaining bags will be displayed) is completely empty.




