+ Reply to Thread
Results 1 to 6 of 6

Can Countifs and Sumproduct work together?

Hybrid View

  1. #1
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Exclamation Can Countifs and Sumproduct work together?

    I have a column I4:I80. In this column, all the cells have either text "NF" or "F". Some of these cells are coloured light grey and some are not. I want to have a formula that will count the number of NF or F that are only in the light grey cells.

    This is the formula I used for counting the number of "F" :

    =countifs(I4:I80,"=F", I4:I80,"=sumproduct(--(colorindex(I4:I80)=colorindex(I87)))")

    I87 is the cell where I have the light grey colour.

    Why does the formula return a 0 (zero) for both NF and F? (I have 20 NF and 8 F in light grey coloured cells.)

    Can anyone help? Thank you.
    Last edited by KSChan; 11-08-2012 at 08:21 AM. Reason: Mistakes in the above countifs formula.

  2. #2
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Can Countifs and Sumproduct work together?

    where is colorindex() coming from?
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  3. #3
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: Can Countifs and Sumproduct work together?

    Quote Originally Posted by martindwilson View Post
    where is colorindex() coming from?
    Thanks for your reply. The Colorindex function is as follow:

    '---------------------------------------------------------------------
    ' ColorIndex Function
    '---------------------------------------------------------------------
    ' Function: Returns the colorindex of the supplied range
    ' Synopsis: Initially, gets a colorindex value for black and white
    ' from the activeworkbook colour palette
    ' Then works through each cell in the supplied range and
    ' determines the colorindex, and adds to array
    ' Finishes by returning acumulated array
    ' Variations: Determines cell colour (interior) or text colour (font)
    ' Default is cell colour
    ' Constraints: Does not count colours set by conditional formatting
    '---------------------------------------------------------------------
    ' Author: Bob Phillips
    ' Additions for ranges suggested by Harlan Grove
    '---------------------------------------------------------------------


    '---------------------------------------------------------------------
    Function ColorIndex(rng As Range, _
    Optional text As Boolean = False) As Variant
    '---------------------------------------------------------------------
    Application.Volatile
    Dim cell As Range, row As Range
    Dim i As Long, j As Long
    Dim iWhite As Long, iBlack As Long
    Dim aryColours As Variant

    If rng.Areas.Count > 1 Then
    ColorIndex = CVErr(xlErrValue)
    Exit Function
    End If

    iWhite = WhiteColorindex(rng.Worksheet.Parent)
    iBlack = BlackColorindex(rng.Worksheet.Parent)

    If rng.Cells.Count = 1 Then
    If text Then
    aryColours = DecodeColorIndex(rng, True, iBlack)
    Else
    aryColours = DecodeColorIndex(rng, False, iWhite)
    End If

    Else
    aryColours = rng.Value
    i = 0

    For Each row In rng.Rows
    i = i + 1
    j = 0

    For Each cell In row.Cells
    j = j + 1

    If text Then
    aryColours(i, j) = _
    DecodeColorIndex(cell, True, iBlack)
    Else
    aryColours(i, j) = _
    DecodeColorIndex(cell, False, iWhite)
    End If

    Next cell

    Next row

    End If

    ColorIndex = aryColours

    End Function

    '---------------------------------------------------------------------
    Private Function WhiteColorindex(oWB As Workbook)
    '---------------------------------------------------------------------
    Dim iPalette As Long
    WhiteColorindex = 0
    For iPalette = 1 To 56
    If oWB.Colors(iPalette) = &HFFFFFF Then
    WhiteColorindex = iPalette
    Exit Function
    End If
    Next iPalette
    End Function

    '---------------------------------------------------------------------
    Private Function BlackColorindex(oWB As Workbook)
    '---------------------------------------------------------------------
    Dim iPalette As Long
    BlackColorindex = 0
    For iPalette = 1 To 56
    If oWB.Colors(iPalette) = &H0 Then
    BlackColorindex = iPalette
    Exit Function
    End If
    Next iPalette
    End Function

    '---------------------------------------------------------------------
    Private Function DecodeColorIndex(rng As Range, _
    text As Boolean, _
    idx As Long)
    '---------------------------------------------------------------------
    Dim iColor As Long
    If text Then
    iColor = rng.Font.ColorIndex
    Else
    iColor = rng.Interior.ColorIndex
    End If
    If iColor < 0 Then
    iColor = idx
    End If
    DecodeColorIndex = iColor
    End Function

    '---------------------------------------------------------------------
    ' End of ColorIndex Function
    '---------------------------------------------------------------------

  4. #4
    Registered User
    Join Date
    10-06-2012
    Location
    Singapore
    MS-Off Ver
    Excel 365
    Posts
    50

    Re: Can Countifs and Sumproduct work together?

    Thanks for your reply. The Colorindex function is as follow:

    '---------------------------------------------------------------------
    ' ColorIndex Function
    '---------------------------------------------------------------------
    ' Function: Returns the colorindex of the supplied range
    ' Synopsis: Initially, gets a colorindex value for black and white
    ' from the activeworkbook colour palette
    ' Then works through each cell in the supplied range and
    ' determines the colorindex, and adds to array
    ' Finishes by returning acumulated array
    ' Variations: Determines cell colour (interior) or text colour (font)
    ' Default is cell colour
    ' Constraints: Does not count colours set by conditional formatting
    '---------------------------------------------------------------------
    ' Author: Bob Phillips
    ' Additions for ranges suggested by Harlan Grove
    '---------------------------------------------------------------------


    '---------------------------------------------------------------------
    Function ColorIndex(rng As Range, _
    Optional text As Boolean = False) As Variant
    '---------------------------------------------------------------------
    Application.Volatile
    Dim cell As Range, row As Range
    Dim i As Long, j As Long
    Dim iWhite As Long, iBlack As Long
    Dim aryColours As Variant

    If rng.Areas.Count > 1 Then
    ColorIndex = CVErr(xlErrValue)
    Exit Function
    End If

    iWhite = WhiteColorindex(rng.Worksheet.Parent)
    iBlack = BlackColorindex(rng.Worksheet.Parent)

    If rng.Cells.Count = 1 Then
    If text Then
    aryColours = DecodeColorIndex(rng, True, iBlack)
    Else
    aryColours = DecodeColorIndex(rng, False, iWhite)
    End If

    Else
    aryColours = rng.Value
    i = 0

    For Each row In rng.Rows
    i = i + 1
    j = 0

    For Each cell In row.Cells
    j = j + 1

    If text Then
    aryColours(i, j) = _
    DecodeColorIndex(cell, True, iBlack)
    Else
    aryColours(i, j) = _
    DecodeColorIndex(cell, False, iWhite)
    End If

    Next cell

    Next row

    End If

    ColorIndex = aryColours

    End Function

    '---------------------------------------------------------------------
    Private Function WhiteColorindex(oWB As Workbook)
    '---------------------------------------------------------------------
    Dim iPalette As Long
    WhiteColorindex = 0
    For iPalette = 1 To 56
    If oWB.Colors(iPalette) = &HFFFFFF Then
    WhiteColorindex = iPalette
    Exit Function
    End If
    Next iPalette
    End Function

    '---------------------------------------------------------------------
    Private Function BlackColorindex(oWB As Workbook)
    '---------------------------------------------------------------------
    Dim iPalette As Long
    BlackColorindex = 0
    For iPalette = 1 To 56
    If oWB.Colors(iPalette) = &H0 Then
    BlackColorindex = iPalette
    Exit Function
    End If
    Next iPalette
    End Function

    '---------------------------------------------------------------------
    Private Function DecodeColorIndex(rng As Range, _
    text As Boolean, _
    idx As Long)
    '---------------------------------------------------------------------
    Dim iColor As Long
    If text Then
    iColor = rng.Font.ColorIndex
    Else
    iColor = rng.Interior.ColorIndex
    End If
    If iColor < 0 Then
    iColor = idx
    End If
    DecodeColorIndex = iColor
    End Function

    '---------------------------------------------------------------------
    ' End of ColorIndex Function
    '---------------------------------------------------------------------

  5. #5
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,047

    Re: Can Countifs and Sumproduct work together?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  6. #6
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Can Countifs and Sumproduct work together?

    Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1