(VBA) Refresh cell if another cell in a range changes colour

KVexcel

New Member
Joined
Sep 13, 2022
Messages
14
Office Version
  1. 2021
Platform
  1. Windows
So I have this function that counts cells of a color on a range
VBA Code:
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
  If rCell.Interior.ColorIndex = CountColorValue Then
    TotalCount = TotalCount + 1
  End If
Next rCell
GetColorCount = TotalCount
End Function
I want the function cell to be refreshed each time a cell on the given range changes colour. Also, is there a way to incorporate this inside the function code or does it have to be a seperate sub ?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
There is no way to do that as there is no-way to pick-up the fact that a cell has had the colour changed. The best you could do would be to use a selection change event, but for that to work you would need to select another cell after changing the colour.
 
Upvote 0
I use a macro recorder to insert the data in another program, and it makes the adress yellow as a last step so I can have a visual representation that it has finished with this row. So it selects other cells all the time after changing colours.


TAGS TEMPLATE.xlsm
ABC
1TAG NAMEADRESSTOTAL TAG COUNT:
2AC CONTROL PANEL COMMON ALARM18651074
3ACH_AC POWER FAILURE1041
4ACH_AH DRUM CLUTCH IN1062READY TAGS:
5ACH_AH DRUM CLUTCH OUT10632
6ACH_AH DRUM HOLD10600.343488494
EASY TAGS
Cell Formulas
RangeFormula
A2:B1075A2=SORT(FILTER(calc!$A2:$B5001, (calc!$A2:$A5001<>"")+(calc!$B2:$B5001<>"")))
C2C2=5000-COUNTBLANK($A2:$A5001)
C5C5=GetColorCount(B2:B5001,$B$5002)
C6C6=RAND()
Dynamic array formulas.


C6 is there just to check visually if C5 is getting refreshed, I tried with
VBA Code:
Sub Calculate_range()
    Worksheets("EASY TAGS").Range("C5:C6").Calculate
    Application.OnTime DateAdd("s", 1, Now), "Calculate_range"
End Sub
but unfortunately it didn't work
 
Upvote 0
Are the cells being coloured manually, or by a macro?
 
Upvote 0
By a macro, but it has mouse tracking inside the loop, so it actually clicks on the fill colour button on excel.
It's not an excel macro.
 
Upvote 0
In that case after you have run the macro, why not run another that will update the count?
 
Upvote 0
The mouse & keyboard macro does Alt-Tab to other applications, and it takes ~4,5seconds per row. With my datasets (up to 3000 rows) it takes quite a while to finish, and sometimes I may need to stop it and resume later. So i would like the count te be refreshed constantly.
I searched the forum based on your first answer and found this post where you also commented
Selection Change event
Maybe I need something like this ?
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rPrintArea As Range

 If Not Intersect(Target, Range("B2:B5001")) Is Nothing Then
    Worksheets("EASY TAGS").Range("C5:C6").Calculate
 End If
 
 End Sub
 
Upvote 0
I tried and it didn't work. This should refresh C5:C6 whenever you select cells on the B2:B5001 range, right?
 
Upvote 0
You will also need to make the UDF volatile like
VBA Code:
Function GetColorCount(CountRange As Range, CountColor As Range)
Dim CountColorValue As Integer
Dim TotalCount As Integer
Application.Volatile
CountColorValue = CountColor.Interior.ColorIndex
Set rCell = CountRange
For Each rCell In CountRange
  If rCell.Interior.ColorIndex = CountColorValue Then
    TotalCount = TotalCount + 1
  End If
Next rCell
GetColorCount = TotalCount
End Function
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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