+ Reply to Thread
Results 1 to 8 of 8

COUNTIF formula to look up text values and the colour of the cell

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-02-2003
    Location
    Birmingham, UK
    MS-Off Ver
    Office 365
    Posts
    127

    COUNTIF formula to look up text values and the colour of the cell

    Is there a way I can use a formula like COUNTIF to scan a large spreadsheet to return the number of occurrences of a value say 'XY' where the cell colour is also highlighted in Yellow ? At the same time, ignoring all the occurences where the cell isn't highlighted ?

    Any help would be gratefully received.

    Tdub
    Terry

    "... I thought I was a power user of Excel until I came onto these boards..."

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,227

    Re: COUNTIF formula to look up text values and the colour of the cell

    Hi twaccess,

    Counting values in cells is easy but the color something is, is very hard. If you had the formula that colors these cells yellow and add that criteria to a CountIfS function you might succeed.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,273

    Re: COUNTIF formula to look up text values and the colour of the cell

    Quote Originally Posted by MarvinP View Post
    Hi twaccess,

    Counting values in cells is easy but the color something is, is very hard. If you had the formula that colors these cells yellow and add that criteria to a CountIfS function you might succeed.
    It's not hard at all if the colour was applied using conditional formatting, as in such cases, the CF formula can be incorporated into the counting formula. If it has been applied manually, then it's harder.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  4. #4
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,603

    Re: COUNTIF formula to look up text values and the colour of the cell

    Quote Originally Posted by AliGW View Post
    It's not hard at all if the colour was applied using conditional formatting, as in such cases, the CF formula can be incorporated into the counting formula.
    Isn't that what Marvin implied?

    Quote Originally Posted by MarvinP View Post
    Hi twaccess,

    If you had the formula that colors these cells yellow and add that criteria to a CountIfS function you might succeed.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2503 (Windows 11 Home 24H2 64-bit)
    Posts
    90,273

    Re: COUNTIF formula to look up text values and the colour of the cell

    Quote Originally Posted by Pepe Le Mokko View Post
    Isn't that what Marvin implied?
    Implied, yes, but didn't actually say. I was providing clarity in case the OP was unable to infer what Marvin was implying.

  6. #6
    Forum Contributor
    Join Date
    03-14-2013
    Location
    England
    MS-Off Ver
    2007
    Posts
    191

    Re: COUNTIF formula to look up text values and the colour of the cell

    You would have to create a custom fiction with vba to count cells by colour. It works by comparing them to the colour of another cell, so you would need to have an input cell where you set the background colour to the one you want to find. Google 'Excel count cells by colour'.

  7. #7
    Forum Contributor
    Join Date
    02-07-2013
    Location
    Karachi, Pakistan
    MS-Off Ver
    Excel 2007
    Posts
    290

    Re: COUNTIF formula to look up text values and the colour of the cell

    Hi,

    Little painful, but non-vba solution is...

    Suppose your data in column B, add a helper column (e.g. column C) with any value (say 1) to your highlighted cell.
    Now use:
    =COUNTIFS(B3:B10,"XY",C3:C10,1)
    adjust the range to suite.

    Blessing.

  8. #8
    Banned User!
    Join Date
    02-05-2015
    Location
    San Escobar
    MS-Off Ver
    any on PC except 365
    Posts
    12,168

    Re: COUNTIF formula to look up text values and the colour of the cell

    module1:
    '---------------------------------------------------------------------
    ' 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
    '---------------------------------------------------------------------
    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
    and
    =SUMPRODUCT(--(ColorIndex(A1:G100)=6),--(A1:G100="XY"))
    I don't see in original post: VBA is prohibited

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Formula to check colour of text or colour of background
    By Wonderful World in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-07-2014, 01:49 AM
  2. [SOLVED] Macro to change text colour of adjacent cell to the same text colour as target cell
    By kayoke in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-28-2012, 04:18 AM
  3. Replies: 3
    Last Post: 12-05-2011, 09:07 AM
  4. Replies: 2
    Last Post: 06-07-2011, 07:00 AM
  5. Coloured Text In Cells Formula So Excel Knows What Colour Is Used & Uses Correct Cell
    By mightymax76 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-20-2010, 09:24 AM
  6. Count cell values based on text colour
    By mundo in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-22-2008, 09:25 AM
  7. Getting range of cells with specific text colour, then using in a COUNTIF formulae
    By cheekyflash in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-11-2006, 08:58 PM

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