COUNTIF to count cells with conditional formatting

Moosles

New Member
Joined
Apr 1, 2021
Messages
23
Office Version
  1. 2019
Platform
  1. Windows
I have an enormous range of data (10k rows x 10k columns) with conditional formatting applied to all cells in this range. The range is something like the below, which is a concatenate of the row and column header values:

000100020003000400050006
0001000100010001000200010003000100040001000500010006
0002000200010002000200020003000200040002000500020006
0003000300010003000200030003000300040003000500030006
0004000400010004000200040003000400040004000500040006
0005000500010005000200050003000500040005000500050006
0006000600010006000200060003000600040006000500060006

I want to do a COUNTIF to see how many cells in each row have conditional formatting applied. The conditional formatting returns RGB(255, 204, 255) when the condition is met. The condition is a MATCH formula that looks at a separate tab to see if the concatenate value exists in that tab, so the COUNTIF would not be able to use the condition as a criteria. I was looking at using a custom formula in VBA:

VBA Code:
Function COUNT_COLOR(RANGE As RANGE, COLOR As RANGE)
Dim COLORC As Integer
Dim COUNTT As Integer
COLORC = COLOR.Interior.ColorIndex
For Each IC In RANGE
If IC.Interior.ColorIndex = COLORC Then
COUNTT = COUNTT + 1
End If
Next IC
COUNT_COLOR = COUNTT
End Function

But this only works for cells that explicitly have the color applied, rather than ones that have conditional formatting applied. What would I need to do to make this work? Thanks!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
You would need the DisplayFormat property, but that won't work in a UDF. You could probably use SUMPRODUCT instead to count values that match the other tab rather than relying on counting the CF.
 
Upvote 0
Can you show us a bit of the other tab so we can see what the data there is like and how it is laid out?

What is the actual CF formula in that top-left concatenated cell in your post above?
 
Upvote 0
Can you show us a bit of the other tab so we can see what the data there is like and how it is laid out?

What is the actual CF formula in that top-left concatenated cell in your post above?
Sure, the lookup tab is just a data dump of over 500k rows which has the concatenate value in column A and various other info in the other columns:

ConcatenateInfo AInfo B
00030005gdsfgdgjhfjytbdf
00020001fdgsdgsfdhfhshrthtth
00060004fghfghfgdhdgdgrtyhgg
00030002eafregfdgfgdfgdgfghwrgwerg
00050006retrtdhzvgzffgwdghh
00010004fdgjryueysghdgdfghrthwg
00060001agsdgrteyujhsrbcvbgeggaeg
00040003gsdgrteurthrdfgdfgwreytqt
00020002hfgnhvnbxvgdfgfdshreytytwreg

The Conditional Formatting formula is =MATCH(D3,Lookup!$G:$G,0).
 
Upvote 0
Thanks for the extra information. I'm a little confused though. There seems to be a contradiction about the concatenated values in 'Lookup' or else I am completely mis-understanding. :confused:
the lookup tab ... has the concatenate value in column A

The Conditional Formatting formula is =MATCH(D3,Lookup!$G:$G,0).

Could this UDF work for you then, rather than trying to count the coloured cells?

VBA Code:
Function CountConcatMatches(rVals As Range, rLookup As Range) As Long
  Dim d As Object
  Dim a As Variant
  Dim uba2 As Long, i As Long, j As Long, k As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With rLookup.EntireColumn
    a = .Resize(.Cells(Rows.Count, 1).End(xlUp).Row).Value
  End With
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  a = rVals.Value
  uba2 = UBound(a, 2)
  For i = 1 To UBound(a)
    For j = 1 To uba2
      If Len(a(i, j)) > 0 Then k = k + d(a(i, j))
    Next j
  Next i
  CountConcatMatches = k
End Function

I have assumed the concatenated values are in col G per the CF formula, not col A per your written description.
(Using XL2BB for your sample data would make it simple to know exactly what columns/rows are used. You might consider it for the future.)

Moosles.xlsm
G
1
200030005
300020001
400060004
500030002
600050006
700010004
800060001
900040003
1000020002
1199999999
12
Lookup


Moosles.xlsm
CDEFGHIJK
1
2000100020003000400050006Count
300010001000100010002000100030001000400010005000100069
40002000200010002000200020003000200040002000500020006
50003000300010003000200030003000300040003000500030006
60004000400010004000200040003000400040004000500040006
70005000500010005000200050003000500040005000500050006
80006000600010006000200060003000600040006000500060006
Sheet1
Cell Formulas
RangeFormula
K3K3=CountConcatMatches(D3:I8,Lookup!G:G)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D3:I8Expression=MATCH(D3,Lookup!$G:$G,0)textNO
 
Upvote 0
Solution
Wouldn't something like this work:

Excel Formula:
=SUMPRODUCT(--ISNUMBER(MATCH(D3:I8,Lookup!$G:$G,0)))
 
Upvote 0
Wouldn't something like this work:

Excel Formula:
=SUMPRODUCT(--ISNUMBER(MATCH(D3:I8,Lookup!$G:$G,0)))
OP had 10,000 rows x 10,000 columns. With my (fairly old) machine it took nearly 20 seconds to calculate my UDF. It just fell over trying to do SUMPRODUCT that big.
 
Upvote 0
Thanks for the extra information. I'm a little confused though. There seems to be a contradiction about the concatenated values in 'Lookup' or else I am completely mis-understanding. :confused:




Could this UDF work for you then, rather than trying to count the coloured cells?

VBA Code:
Function CountConcatMatches(rVals As Range, rLookup As Range) As Long
  Dim d As Object
  Dim a As Variant
  Dim uba2 As Long, i As Long, j As Long, k As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  With rLookup.EntireColumn
    a = .Resize(.Cells(Rows.Count, 1).End(xlUp).Row).Value
  End With
  For i = 1 To UBound(a)
    d(a(i, 1)) = 1
  Next i
  a = rVals.Value
  uba2 = UBound(a, 2)
  For i = 1 To UBound(a)
    For j = 1 To uba2
      If Len(a(i, j)) > 0 Then k = k + d(a(i, j))
    Next j
  Next i
  CountConcatMatches = k
End Function

I have assumed the concatenated values are in col G per the CF formula, not col A per your written description.
(Using XL2BB for your sample data would make it simple to know exactly what columns/rows are used. You might consider it for the future.)

Moosles.xlsm
G
1
200030005
300020001
400060004
500030002
600050006
700010004
800060001
900040003
1000020002
1199999999
12
Lookup


Moosles.xlsm
CDEFGHIJK
1
2000100020003000400050006Count
300010001000100010002000100030001000400010005000100069
40002000200010002000200020003000200040002000500020006
50003000300010003000200030003000300040003000500030006
60004000400010004000200040003000400040004000500040006
70005000500010005000200050003000500040005000500050006
80006000600010006000200060003000600040006000500060006
Sheet1
Cell Formulas
RangeFormula
K3K3=CountConcatMatches(D3:I8,Lookup!G:G)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
D3:I8Expression=MATCH(D3,Lookup!$G:$G,0)textNO
This is perfect for my needs, thanks Peter!
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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