Forum Discussion
vba Function for COUNT Condition Color Cells
- Aug 30, 2022
1) Copy the following code into a standard module.
Sub CountColors() Dim r As Long Dim m As Long Application.ScreenUpdating = False m = Range("E:AJ").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row For r = 4 To m Cells(r, 37).Value = CountRed(r) Next r Application.ScreenUpdating = True End Sub Function CountRed(r As Long) As Long Dim c As Long For c = 5 To 36 ' E to AJ If Cells(r, c).DisplayFormat.Interior.Color = vbRed Then CountRed = CountRed + 1 End If Next c End Function
2) Run the Countcolors macro once to populate column AK. It can easily be expanded if you want to count green and grey in other columns.
3) Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module.
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range Dim r As Long If Not Intersect(Range("E4:AJ" & Rows.Count), Target) Is Nothing Then Application.ScreenUpdating = False Application.EnableEvents = False For Each rng In Intersect(Range("E4:AJ" & Rows.Count), Target).Rows r = rng.Row Cells(r, 37).Value = CountRed(r) Next rng Application.EnableEvents = True Application.ScreenUpdating = True End If End Sub
This will update the count automatically when you edit cells in columns E to AJ.
Each column has different conditional formatting rule for each colour, if I refer to all of those conditions, the end function will be very messy π
Can you give me more details about the VBA code that will fill the column with the count for Red? If possible some example of the code?
1) Copy the following code into a standard module.
Sub CountColors()
Dim r As Long
Dim m As Long
Application.ScreenUpdating = False
m = Range("E:AJ").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For r = 4 To m
Cells(r, 37).Value = CountRed(r)
Next r
Application.ScreenUpdating = True
End Sub
Function CountRed(r As Long) As Long
Dim c As Long
For c = 5 To 36 ' E to AJ
If Cells(r, c).DisplayFormat.Interior.Color = vbRed Then
CountRed = CountRed + 1
End If
Next c
End Function
2) Run the Countcolors macro once to populate column AK. It can easily be expanded if you want to count green and grey in other columns.
3) Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the following code into the worksheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Dim r As Long
If Not Intersect(Range("E4:AJ" & Rows.Count), Target) Is Nothing Then
Application.ScreenUpdating = False
Application.EnableEvents = False
For Each rng In Intersect(Range("E4:AJ" & Rows.Count), Target).Rows
r = rng.Row
Cells(r, 37).Value = CountRed(r)
Next rng
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
This will update the count automatically when you edit cells in columns E to AJ.
- ZoolanderNov 11, 2022Copper Contributor
How can you manipulate this code to spit out the information by column instead of rows?
Example it spits out on column AK for each row
i want it to spit out in the last row for each column
May you manipulate the code for that? Canβt seem to get it to work.
- HansVogelaarNov 12, 2022MVP
Could you explain in more detail what you want? Thanks in advance.
- ZoolanderNov 12, 2022Copper ContributorSo in your solution the output displays in column AK in rows (AK4,AK5,AK6,AK7, etc)
Can you make it so it reads the conditional formatting by column?
In which the output could display at the bottom. For example the output would be on E100, F100, G100, H100, etc
- NSrebroAug 30, 2022Copper ContributorHans, you are genius! Your step-by-step explanation is brilliant and the final outcome works very smoothly!
Thanks a lot for this!
Regards,
Nik