VBA & Macro Problems

How to Pack Items into Bags by Weight and Category Using Excel VBA

Maira MehtabMaira Mehtab Nov 10, 2026 869 views

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.

How to Pack Items into Bags by Weight and Category Using Excel VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the top menu, click on Insert, then select Module to create a blank workspace for your macro code.

3
Paste the Packing 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).

4
Run the Macro

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.

Use a VBA Macro with Dictionary Objects to Group Items
Data Setup: The code assumes item categories are located in Column C, weights in Column D, and outputs the assigned bag numbers in Column F starting from row 2.
Advanced Macro Support

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. 1. Open Your Workbook: Launch WPS Office Spreadsheet and open your macro-enabled workbook containing the inventory data.
  2. 2. Access the VBA Editor: Navigate to the Tools tab on the ribbon and click on Macro, then select Visual Basic Editor.
  3. 3. Execute the Code: Paste your bag-packing VBA code into a new module and hit Run to assign bag numbers instantly.
Full compatibility with Microsoft Excel .xlsm and .xlsb formats.Built-in VBA editor equipped with object browsers and debugging tools.Lightweight software that processes large dataset loops and dictionary objects efficiently.
microsoft office alternative - wps office

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".