Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9130733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:00:55+00:00 2026-06-17T08:00:55+00:00

The requirement is a piece of code which can count instances of a string

  • 0

sample excel

The requirement is a piece of code which can count instances of a string i.e(ABC,DEF,GHK), based on if they are present in a colored cell or not and put the results in a cell below as shown .

Can anyone please advise?

I tried a sample code

Sub Color()

Dim varCounter As String
Dim color As Integer
Dim nocolor As Integer

Range("E5").Select
color= 0
nocolor= 0

      Do Until Selection.Value = ""
                  If Selection.Font.Color = RGB(255, 0, 0) Then
                   color= color+ 1
                     Else
                   nocolor= nocolor+ 1
                   End If
                   Selection.Offset(1, 0).Select
        Loop
  Range("E47").Select
  Selection.Value = no


  Range("E48").Select
Selection.Value = color

End Sub

This a very simple code which check if the text font is colored or not , buut I couldn’t find anything which check for the background color of the cell.

I also tried excel formula , but that with that I can only search for text and count , it doesn’t count based on background color of the cell.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:00:56+00:00Added an answer on June 17, 2026 at 8:00 am

    Here’s a simple user-defined function. You can put it in a regular module. You can then call it from any worksheet in the workbook it’s in:

    Public Function CountByColorAndText(rng As Excel.Range, SearchText As String, CountColored As Boolean) As Long
    Dim cell As Excel.Range
    Dim CellCount As Long
    
    For Each cell In rng
        If cell.Value = SearchText Then
             If (cell.Interior.ColorIndex = -4142 And Not CountColored) Or _
               (cell.Interior.ColorIndex <> -4142 And CountColored) Then
                CellCount = CellCount + 1
            End If
        End If
    Next cell
    CountByColorAndText = CellCount
    End Function
    

    It takes three arguments: the range to evaluate, the string to search for and whether you’re counting colored (or uncolored) cells:

    enter image description here

    So, above, in column E the formula is:

    =CountByColorAndText($A$2:$A$13,$D3,FALSE)
    

    In column F it’s the same except the last argument, CountColored is TRUE.

    I don’t write many user-defined functions, so somebody may come along and point out problems or improvements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a requirement that every piece of code that makes it into production
I am currently working a piece of code which needs to send an attachment
I am using simple model which is a very neat piece of code but
My requirement is that a piece of code should be triggered immediately whenever internet
I have a piece of unix software written in C++ which can be build
All headers are included as per requirement. This piece of code below is working
I'm working with a piece of code that applies a regex to a string
Is there any class, library or some piece of code which will help me
Requirement : Should update MySQL database with that of MS Sql Server updates which
My requirement is parts of text should be displayed as bold and colored within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.