Blair
Have restored this and added a Maximum value as well
[pre]
Code:
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM_MIN As Integer = 0, Optional iSign As Integer = 0)
Dim rCell As Range
Dim lCol As Long
Dim vResult
Dim min_val As Variant
min_val = 1E+16 ' An arbitrary high-value
''''''''''''''''''''''''''''''''''''''
'Written by Ozgrid Business Applications
'www.ozgrid.com
'Modified by Hui
'SUM_Min
'0 - Sum cells
'1 - Count
'2 - Minimum
'3 - Maximum
'''''''''''''''''''''''''''''''''''''''
lCol = rColor.Interior.ColorIndex
If SUM_MIN = 0 Then ' Sum cells. = Default Value
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
If iSign = -1 And rCell.Value < 0 Then
vResult = vResult + rCell.Value
ElseIf iSign = 1 And rCell.Value > 0 Then
vResult = vResult + rCell.Value
ElseIf iSign = 0 Then
vResult = vResult + rCell.Value
End If
End If
Next rCell
ElseIf SUM_MIN = 1 Then ' Count cells.
vResult = 0
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
If iSign = -1 And rCell.Value < 0 Then
vResult = vResult + 1
ElseIf iSign = 1 And rCell.Value > 0 Then
vResult = vResult + 1
ElseIf iSign = 0 Then
vResult = vResult + 1
End If
End If
Next rCell
ElseIf SUM_MIN = 2 Then ' extract Minimum
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
If iSign = -1 And rCell.Value < 0 And rCell < min_val Then
min_val = rCell
ElseIf iSign = 1 And rCell.Value >= 0 And rCell < min_val Then
min_val = rCell
ElseIf iSign = 0 And rCell < min_val Then
min_val = rCell
End If
End If
Next rCell
vResult = min_val
ElseIf SUM_MIN = 3 Then ' extract Maximum
min_val = -min_val
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
If iSign = -1 And rCell.Value < 0 And rCell.Value > min_val Then
min_val = rCell
ElseIf iSign = 1 And rCell.Value >= 0 And rCell > min_val Then
min_val = rCell
ElseIf iSign = 0 And rCell > min_val Then
min_val = rCell
End If
End If
Next rCell
vResult = min_val
End If
ColorFunction = vResult
End Function
[/pre]
in use:
=ColorFunction(Color Match Cell, Cells to Sum, Func , iSign)
Color Match Cell = Cell with a color which you want to match
Cells to Sum = Your range you want to sum/min
Func: =0 Sum values (Default)
=1 Count
=2 Minumum
=3 Maximum
iSign: -1 Sum Negatives, 1 Sum positives, 0 or missing Sum all
in use:
=ColorFunction(B9,D1:M1,0,-1) ' Sum negatives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,0,1) ' Sum positives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,0,0) ' Sum all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,,) ' Sum all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1) ' Sum all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,1,-1) ' Count negatives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,1,1) ' Count positives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,1,0) ' Count all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,1) ' Count all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,2,-1) ' Minimum of all negatives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,2,1) ' Minimum of all positives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,2,0) ' Minimum of all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,2) ' Minimum of all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,3,-1) ' Maximum of all negatives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,3,1) ' Maximum of all positives between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,3,0) ' Maximum of all cells between D1:M1 that match color in B9
=ColorFunction(B9,D1:M1,3) ' Maximum of all cells between D1:M1 that match color in B9