# Min/MaxifbyColor

#### quin

##### New Member
I have this Function I found at Chip Pearson's site for SumIfByColor.
how would one modify it to be a MaxIfByColor or MinIfByColor?
I thought it would be as easy as replacing sum with min/max but i was mistaken.

here is the code:
Code:
``````Function SumIfByColor(InRange As Range, _
WhatColorIndex As Integer, SumRange As Range, _
Optional OfText As Boolean = False) As Variant
'
' This function will return the SUM of the values of cells in
' SumRange where the corresponding cell in InRange has a background
' color (or font color, if OfText is true) equal to WhatColorIndex.
'
Dim OK As Boolean
Dim Ndx As Long

Application.Volatile True

If (InRange.Rows.Count <> SumRange.Rows.Count) Or _
(InRange.Columns.Count <> SumRange.Columns.Count) Then
SumIfByColor = CVErr(xlErrRef)
Exit Function
End If

For Ndx = 1 To InRange.Cells.Count
If OfText = True Then
OK = (InRange.Cells(Ndx).Font.ColorIndex = WhatColorIndex)
Else
OK = (InRange.Cells(Ndx).Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(SumRange.Cells(Ndx).Value) Then
SumIfByColor = SumIfByColor + SumRange.Cells(Ndx).Value
End If
Next Ndx

End Function``````

thanks

quincy

### Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

#### Scott Huish

##### MrExcel MVP
Perhaps:

Code:
``````Function MaxIfByColor(InRange As Range, _
WhatColorIndex As Integer, MaxRange As Range, _
Optional OfText As Boolean = False) As Variant
'
' This function will return the MAX of the values of cells in
' MaxRange where the corresponding cell in InRange has a background
' color (or font color, if OfText is true) equal to WhatColorIndex.
'
Dim OK As Boolean
Dim Ndx As Long
Dim j

Application.Volatile True

If (InRange.Rows.Count <> MaxRange.Rows.Count) Or _
(InRange.Columns.Count <> MaxRange.Columns.Count) Then
MaxIfByColor = CVErr(xlErrRef)
Exit Function
End If

For Ndx = 1 To InRange.Cells.Count
If OfText = True Then
OK = (InRange.Cells(Ndx).Font.ColorIndex = WhatColorIndex)
Else
OK = (InRange.Cells(Ndx).Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(MaxRange.Cells(Ndx)) Then
If IsEmpty(j) Then
j = MaxRange.Cells(Ndx)
Else
If MaxRange.Cells(Ndx) > j Then _
j = MaxRange.Cells(Ndx)
End If
End If
Next Ndx
MaxIfByColor = j
End Function``````

Code:
``````Function MinIfByColor(InRange As Range, _
WhatColorIndex As Integer, MinRange As Range, _
Optional OfText As Boolean = False) As Variant
'
' This function will return the MIN of the values of cells in
' MinRange where the corresponding cell in InRange has a background
' color (or font color, if OfText is true) equal to WhatColorIndex.
'
Dim OK As Boolean
Dim Ndx As Long
Dim j

Application.Volatile True

If (InRange.Rows.Count <> MinRange.Rows.Count) Or _
(InRange.Columns.Count <> MinRange.Columns.Count) Then
MinIfByColor = CVErr(xlErrRef)
Exit Function
End If

For Ndx = 1 To InRange.Cells.Count
If OfText = True Then
OK = (InRange.Cells(Ndx).Font.ColorIndex = WhatColorIndex)
Else
OK = (InRange.Cells(Ndx).Interior.ColorIndex = WhatColorIndex)
End If
If OK And IsNumeric(MinRange.Cells(Ndx)) Then
If IsEmpty(j) Then
j = MinRange.Cells(Ndx)
Else
If MinRange.Cells(Ndx) < j Then _
j = MinRange.Cells(Ndx)
End If
End If
Next Ndx
MinIfByColor = j
End Function``````

Replies
13
Views
930
Replies
1
Views
276
Replies
0
Views
1K
Replies
3
Views
982
Replies
1
Views
4K

1,136,618
Messages
5,676,844
Members
419,655
Latest member
pd2021vb

### 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.

### Which adblocker are you using?

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

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