VBA Msgbox count cell specific color

Dao Ha Quang

New Member
Joined
Apr 30, 2023
Messages
20
Office Version
  1. 2016
I want to count all the cells with color RGB(255,0,0) on the sheet and display in 1 Msgbox the number of those cells.
If there is no cell with that color, it will notify Msgbox "No cell..."
Thank you
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Try:

VBA Code:
Sub msg_countcolor()
  Dim sh As Worksheet
  Dim f As Range, r As Range
  Dim cell As String
  Dim n As Long
  
  For Each sh In Sheets
    Application.FindFormat.Clear
    Application.FindFormat.Interior.Color = RGB(255, 0, 0)
    Set r = sh.UsedRange
    Set f = r.Find("", , xlValues, xlPart, , , False, , SearchFormat:=True)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        n = n + 1
        Set f = r.Find("", f, xlValues, xlPart, , , False, , SearchFormat:=True)
      Loop Until cell = f.Address
    End If
  Next
  Application.FindFormat.Clear
  If n = 0 Then
    MsgBox "No cell..."
  Else
    MsgBox "Number: " & n
  End If
End Sub

😊
 
Upvote 0
Solution
Try:

VBA Code:
Sub msg_countcolor()
  Dim sh As Worksheet
  Dim f As Range, r As Range
  Dim cell As String
  Dim n As Long
 
  For Each sh In Sheets
    Application.FindFormat.Clear
    Application.FindFormat.Interior.Color = RGB(255, 0, 0)
    Set r = sh.UsedRange
    Set f = r.Find("", , xlValues, xlPart, , , False, , SearchFormat:=True)
    If Not f Is Nothing Then
      cell = f.Address
      Do
        n = n + 1
        Set f = r.Find("", f, xlValues, xlPart, , , False, , SearchFormat:=True)
      Loop Until cell = f.Address
    End If
  Next
  Application.FindFormat.Clear
  If n = 0 Then
    MsgBox "No cell..."
  Else
    MsgBox "Number: " & n
  End If
End Sub

😊
Thank so much
 
Upvote 0

Forum statistics

Threads
1,215,479
Messages
6,125,041
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top