excelvba

Spread positive value across columns


In an Excel spreadsheet, I take a weekly inventory of items and subtract it from a needs list to create orders for the week.

I want:
If difference between G-F is positive, spread the number needed into the four columns.
If G-F is negative then 0.

I can move columns around if needed.

Order Guide Image

I tried a few SpreadEven VBA codes I found online. I was unable to make the needed adjustments to make it work.


Solution

  • This workaround has to variation uses only excel formulas. In A version a separator column is needed between the input columns and the result columns.
    In B version there are two formulas one for the first value, and a second for the other three.

    Version A

    A separator column is in column H.
    Formula in I4 and this formula is copied in all the other cells.

    =IF(($G4-$F4)>0,ROUND(($G4-$F4-SUM($H4:H4))/(COLUMN($F4)+7-COLUMN()),))

    Version B

    Formula in H13 and copy down
    =IF(($G13-$F13)>0,ROUND(($G13-$F13)/4,))

    Formula in I13 and copy the whole range (in examp. I13:K18)
    =IF(($G13-$F13)>0,ROUND(($G13-$F13-SUM($H13:H13))/(COLUMN($I13)+2-COLUMN(H13)),))

    Basically divide the total value to distribute - the already counted values by the remaining number of columns and always round the numbers to an integer value.

    In VBA (if that is a requirement) all these formulas can be assigned with the Range.Formula property.

    enter image description here