This will be non-trivial coding, and should be developed in a very systematic method. Most simple VBA coding is done in a linear way, (consider the whole problem >> develop a solution.)
Lucky for you, I have been on many loading docks in many shipping facilities.
Systematic Method:
Clearly Define the goals;
- Maximize the number of various sized units that can be packed in a given shipping truck's known volume.
- Provide Package assembly and Truck loading instructions for human use.
- *Report size of unused Truck floor space
* indicates desirable, but not required feature
Clearly define the Business Rules;
Packages
- The base of all Packages is a 10cm tall Pallet
- The maximum height of a Package is 120cm
- * Can vary depending on Truck Height?
- A Package can only contain one Type of unit
- A Package can contain Units of different lengths
- Short Lengths will be place on top of Long lengths
- *Multiple stacks of short Units can be placed on a stack of long Units if the combined length of the short stacks are <= to the length of the underlying long Units
- In the Truck, short Packages can be stacked on longer Packages
- * Uneven height long Packages can be stacked on multiple same height short Packages if the combined length of the short Packages is => the long Package
- *Short Packages can be spaced to be = Long packages
Trucks;
- #The Truck Type is known before the code runs
- #The truck Type is only known after the code runs
A stack is one ore more units in a Package
# indicates mutually exclusive
* indicates unknown to me
5.1 & 7 & 7.1 should be included even though it may not be feasible in the first production code.
Provide all the Parameters, (I already see these in the example you uploaded
- Unit Types and sizes
- Pallet Types and sizes *currently, only height. Maybe should include more for Goal # 2.
- Truck Types and sizes
- Package size limits
The above is how I would have started your project. Then and only then would I have pondered about the required code segments. After I had a good idea about them, I would have started coding each of them as a separate piece, but with the ability to have each piece call another as needed.
Now this is the Final stage of my Project.
In that case, you only need to apply the first algorithm I offered after modifying it slightly to
Var PackageType
Var QtyOrdered
Var NumMaxSizePackages
Var LastPackageQty
NumMaxSizePackages = QtyOrdered / VLookup(PackageType, LookUpTable, 2, False)
LastPackageQty = QtyOrdered Mod VLookup(PackageType, LookUpTable, 2, False)
If LastPackageQty.Height < MaxHeight Then
Add ShortLengths Until Packagetype.MaxHeight
Next PackageType.Length.QtyOrdered = (QtyOrdered - ShortLengths.Qty)
In any case, you will need to show us an example book with all your existing code, so we can kludge something up to finish your project.
If, that is, you don't decide to start over, which is a very common choice among professional developers.