[VBA] count cells with color and auto refresh worksheet

DonAndress

Active Member
Joined
Sep 25, 2011
Messages
362
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello.

What I'm trying to achieve today is to count cells with a certain color and then make excel refresh the cell with such sum automatically.

Below I have a code for counting cells with color and it works fine:
Code:
Function CountColorIf(rSample As Range, rArea As Range) As Long    Dim rAreaCell As Range
    Dim lMatchColor As Long
    Dim lCounter As Long


    lMatchColor = rSample.Interior.Color
    For Each rAreaCell In rArea
        If rAreaCell.Interior.Color = lMatchColor Then
            lCounter = lCounter + 1
        End If
    Next rAreaCell
    CountColorIf = lCounter
End Function
as a formula I put
Code:
=countcolorif($K$1,$A$5:$BC$35)
where K1 contains desired color.

But how do I force Excel to refresh this sum automatically every time I add/remove color to any cell within specified range?
Is it even possible?
Any code inside of ThisWorkbook or Sheet1 area of the VBA Project window?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
This will (almost) do what you want. Place this code in the worksheet code area (right-click on the sheet name and click View Code. It doesnt quite work instantly if you change the colur of any cells, but as soon as you select a different cell or range, it will recalculate.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.CalculateFull
End Sub
 
Upvote 0
Yes, this almost works as desired ;)
As you said: it awaits any action after color change, meaning it can be even simple click on any other cell.

Is it possible to force it to refresh instantly?
 
Last edited:
Upvote 0
Not as far as I know - maybe someone else can show a way!
 
Upvote 0
Ok I understand, thank you.

Anyway, could you please help me understand below code, step by step?
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
I'm not familiar with this structure and can't figure out what each part of it does.
Especially when Target is not used anywhere...
 
Upvote 0
Hi there - of course. This code is what is called an event handler. There are several different worksheet events you can watch for (in the vb editor click on the down box next to the selectionchange box to see the list) - and you can write code that runs when that event happens. In this case, the event handler is watching for the focus in your worksheet to switch from one area to another - i.e. you make a 'selection change'. The reason the target is there is twofold - first, you will get a compilation error if you take it out, but secondly it contains a reference to the range you have just selected. This would let you have different code for different selected areas. These event handlers sit in the background and come to life when the event takes place. So, in your case, each time you select a different cell, the application.CalculateFull command is run, which forces your counters to reset.
 
Upvote 0

Forum statistics

Threads
1,215,878
Messages
6,127,506
Members
449,385
Latest member
KMGLarson

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