Macro help.

KurtisT6

New Member
Joined
Aug 21, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
This is My macro:

1691083073294.png


I want to count each cell depending on the colour for a particular range. Below is the example of the macro used:
1691083129826.png

The bracket in red is the range I am searching for the colour specified in cell B1.

This "CountByResult" only works when I click on the cell that contains the macro formula above and press enter.

What I want to happen is for the cell to update automatically as I am changing the colours of a lot of cells and have about 36 colomns with similar formulas using "CountByResult". Therefore, I want it to be fast and not tedious to keep clicking cells and pressing enter.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Check if the following helps you.

1. Add this line to your function:
VBA Code:
Application.Volatile

2. Recommendation:
Do not use variables with the same name as reserved words, you may have errors or not work properly, for example "count":​
Rich (BB code):
Public Function countbyresult(cellrange As Range, targetcell As Range)
  Dim targetcolor As Long, c As Range
  Dim nCount As Long
 
  Application.Volatile
  targetcolor = targetcell.Interior.ColorIndex
  For Each c In cellrange
    If c.Interior.ColorIndex = targetcolor Then nCount = nCount + 1
  Next c
  countbyresult = nCount
End Function

3. Add the following code in the events of your sheet.
VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Application.Calculate
End Sub
Note Sheet Event
Right click the tab of the sheet you want this to work, select view code and paste the code into the window that opens up.​
With the above. you change the color of the cell and selecting any cell will update the calculations.​

4. Note Code Tag:
In future please use code tags when posting code. Instead of an image.​
How to Post Your VBA Code it makes your code easier to read and copy and it also maintains VBA formatting.​


--------------
Cordially
Dante Amor
--------------​
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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