How to Pack Items into Bags by Weight and Category Using Excel VBA
Question details
The user needs a VBA macro to assign sequential bag numbers to items, ensuring each bag does not exceed 2.5 kg and contains items from at least two different categories.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating the grouping and packaging of inventory items based on specific maximum weight limits and strict category distribution rules.
- Observed behavior
- Currently, the user lacks an automated solution to assign bag numbers in column F while simultaneously validating both the 2.5kg weight limit and the multi-category requirement per bag.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have backed up your data, as VBA macro actions cannot be undone using the standard Undo feature.
Use a VBA Macro with Dictionary Objects to Group Items
This solution uses a VBA macro to iterate through rows, calculate cumulative weights, and use a Scripting.Dictionary to ensure multiple categories are included per bag before advancing the bag number.
The provided VBA approach processes items sequentially row by row. It adds item weights to the current bag until the 2.5 kg limit is reached. It simultaneously uses a Scripting.Dictionary object to track unique categories in the current bag.
Note that this is a greedy algorithm. If your dataset requires perfect optimization (minimizing the total number of bags), random packing and complex multi-constraints might require advanced backtracking or external solver methods.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.
In the top menu, click on Insert, then select Module to create a blank workspace for your macro code.
Copy the VBA script that defines variables for 'currentBagWeight' and 'currentBagItems' Dictionary, and paste it into the module window. Ensure the script references your specific data columns (e.g., Column C for Categories, Column D for Weights).
Press F5 or click the Run button (green play icon) in the toolbar. The macro will loop through your dataset and output sequential bag numbers starting at 4001 into Column F.

Run VBA Macros Seamlessly in WPS Office
WPS Office Spreadsheet fully supports VBA and Macro execution, allowing you to automate complex tasks like weight and category packaging with high performance and familiar syntax.
- 1. Open Your Workbook: Launch WPS Office Spreadsheet and open your macro-enabled workbook containing the inventory data.
- 2. Access the VBA Editor: Navigate to the Tools tab on the ribbon and click on Macro, then select Visual Basic Editor.
- 3. Execute the Code: Paste your bag-packing VBA code into a new module and hit Run to assign bag numbers instantly.

Frequently Asked Questions
Why am I getting an error about 'Scripting.Dictionary'?
This error typically occurs on Mac versions of Excel because ActiveX objects like Scripting.Dictionary are Windows-specific. If you are using macOS, you will need to modify the VBA code to use standard VBA Collections instead of the Dictionary object.
How do I change the maximum bag weight in the VBA code?
In the VBA code, locate the conditional line: `If currentBagWeight + itemWeight > 2.5`. Simply change the value `2.5` to your desired maximum weight limit before running the macro.
Can I apply this macro to a different worksheet name?
Yes, you can adapt the macro for different sheets. Update the line `Set ws = ThisWorkbook.Sheets("Sheet1")` and replace "Sheet1" with the actual name of your worksheet, such as "Inventory" or "Data".




