Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

In MS Excel, how do I write this count function ?

I want a cell in MS Excel to count the number cells in range of cells that have a particular background (for instance, orange background); or to count the number of cells with the font defined as bold. How do I write this function?

1 Answer

Relevance
  • Randy
    Lv 4
    1 decade ago
    Favorite Answer

    Add a regular module to your Excel project and add the following functions:

    Function COUNTBOLD(rngTarget As Range)

      Dim rngCell As Range, lngCount As Long

      lngCount = 0

      For Each rngCell In rngTarget

        If rngCell.Font.Bold Then lngCount = lngCount + 1

      Next

      COUNTBOLD = lngCount

    End Function

    Function COUNTCOLOR(rngTarget As Range, Red As Integer, Green As Integer, Blue As Integer)

      Dim rngCell As Range, lngCount As Long

      lngCount = 0

      For Each rngCell In rngTarget

        If rngCell.Interior.Color = RGB(Red, Green, Blue) Then lngCount = lngCount + 1

      Next

      COUNTCOLOR = lngCount

    End Function

    Usage:

    =COUNTBOLD(A1:B2)

    returns the count of all cells in A1:B2 with Bold font turned on

    =COUNTCOLOR(A1:B2,247,150,70)

    returns the count of all cells in A1:B2 with the interior color set to the RGB value of Red=247, Green=150, Blue=70

Still have questions? Get your answers by asking now.