+ Reply to Thread
Results 1 to 3 of 3

VBA Module counts background color, would like it to count font color

  1. #1
    Registered User
    Join Date
    06-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Question VBA Module counts background color, would like it to count font color

    Hi All,

    I'd like to modify this VB function (originally posted elsewhere) to count cells that match the font color rather than the background color. I realize there are other ways to count cells of a certain font color, but I'd really love to see the differences between this code and the final code so I can understand this better (and hopefully not have to post questions too often!).

    This works pretty simply, returning the total cells that match the highlight (background) color: ColorCount(range,cell_to_compare)

    Function CountColor(rColorRange As Range, rColorIndex As Range) As Long
    Dim rCell As Range 'range for loop.
    Dim lColor As Long 'Index of the cells color we want to count.
    Dim lColorCount As Long 'Counter

    lColorCount = 0 'Initialise counter
    lColor = rColorIndex.Interior.ColorIndex 'Set color to count

    'Loop through each cell in the range
    For Each rCell In rColorRange
    If rCell.Interior.ColorIndex = lColor Then lColorCount = lColorCount + 1
    Next rCell

    'Return count to function
    CountColor = lColorCount
    End Function



    Any thoughts?

    Thanks in advance.

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA Module counts background color, would like it to count font color

    Hi, LawBeforeGrace,

    Interior shows the Background while Font is - you guess.

    Please Login or Register  to view this content.
    Please use code-tags when posting.

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Registered User
    Join Date
    06-24-2013
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: VBA Module counts background color, would like it to count font color

    Hi Holger,

    Thank you so much, this works great... and I will use code-tags going forward.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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