Min/MaxifbyColor

quin

New Member
Joined
Sep 29, 2006
Messages
9
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

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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
 
Upvote 0

Forum statistics

Threads
1,215,778
Messages
6,126,841
Members
449,343
Latest member
DEWS2031

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