Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

STL - Formerly Best Training Solutions Through Learning
TrustPilot
Excellent
Request Callback We will call you back
0207 987 3777 Call for assistance
Your Basket Basket is empty
vba

ResolvedVersion 2003

Seigo has attended:
Excel VBA Intro Intermediate course

VBA

How would you count things by colour or format

RE: VBA

Hi Seigo

The following function counts the cells in a range with the same colour background or text depending on the option you set up


Public Function CountByColour(MyRange As Range, _
MyColorIndex As Integer, _
Optional OfText As Boolean = False) As Long

' This function return the number of cells in MyRange with
' a background color, or if OfText is True a font color,
' equal to MyColorIndex.

Dim RangeVar As Range

Application.Volatile True

For Each RangeVar In MyRange.Cells

If OfText = True Then

CountByColour = CountByColour - _
(RangeVar.Font.ColorIndex = MyColorIndex)

Else

CountByColour = CountByColour - _
(RangeVar.Interior.ColorIndex = MyColorIndex)

End If

Next RangeVar

End Function


Hope this helps

Carlos

Excel tip:

COUNT function vs COUNTA function

The COUNTA function works in the same way as the COUNT function, except that it will count cells that contain text (labels) and also cells that contain numbers (values). The COUNT function will only count cells that contain numbers. Blank cells are not counted by either the COUNT or the COUNTA function.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.09 secs.