Optimizing a value by moving cells around using VBA

ggmkp

New Member
Joined
May 3, 2019
Messages
7
Trying to minimize leftovers to be as close as possible to 0 but not negative by shifting inventories around
Inventory qty can only move horizontally (i.e., Inventory 1 can't move to inventory 2 or 3)
Inventory qty is already given, each qty is treated as a whole and can't be combined or merged or added or removed

From this:
624415d1557868453-finding-minimum-value-for-inventory-using-vba-2pa05o7qa8y21.png


To this:
624416d1557868462-finding-minimum-value-for-inventory-using-vba-r2dh9n02n8y21.png


It's like taking a snapshot of inventory and relocating them so that the leftovers for each month is as close to 0 as possible.

I've tried something like this but it doesn't seems to work because it's merging the inventories together by adding the qty and it's only moving to the right..

Code:
[COLOR=#333333]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub test()
Dim rw As Integer, col As Integer
Dim dif As Integer

For rw = 6 To 8
For col = 2 To 7
dif = Application.Min(Cells(4, col), Cells(rw, col))
    If Cells(4, col) > 0 Then
    Cells(rw, col + 1) = Cells(rw, col + 1) + dif
    Cells(rw, col) = Cells(rw, col) - dif
    End If
Next
Next
End Sub</code>[/COLOR]
Appreciate the help.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top