I put it in the macro instead of a formula.. this should do it.
Sub Button1_Click()
Dim c1 As Range
Dim r1 As Range
Set c1 = Range("K27") 'SO#s total for product...
Set r1 = Range("C4:C26") 'PO#s for product...
Dim i As Integer
For i = 1 To 5
Call MakeGray(c1, r1)
Set c1 = c1.Offset(0, 1) 'Move to next column...
Set r1 = r1.Offset(0, 1)
Next i
End Sub
Function MakeGray(total As Range, rng As Range)
Dim cell As Range
Dim s1 As Integer
s1 = 0
Dim k1 As Integer
k = 0
Dim cCol As Integer
Dim cRow As Integer
Dim s2 As Double
Dim s3 As Double
For Each cell In rng
cell.Interior.Pattern = xlNone 'Set blank initially...
k = s1
s1 = s1 + cell.Value
If s1 <= total Then 'If PO qty <= than SO..
If Not cell.Value = "" Then
cell.Interior.Color = 14540253 'Color gray...
End If
Else
cRow = cell.Row - 1
cCol = cell.Offset(0, 16).Column
'Sum of remaining column...
s2 = Application.Sum(Range(Cells(4, cCol), Cells(cell.Row - 1, cCol)))
'Sum of current column...
s3 = Application.Sum(Range(Cells(4, cell.Column), Cells(cell.Row, cell.Column)))
If s2 = 0 Then 'If PO not filled... then include remaining qty...
cell.Offset(0, 16).Value = s3 - total.Value
Else
cell.Offset(0, 16).Value = cell.Value
End If
End If
Next cell
End Function
Bookmarks