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

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

Forum statistics

Threads
1,214,952
Messages
6,122,454
Members
449,083
Latest member
Ava19

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