VBA code to update displayed result

martinsimart

New Member
Joined
Mar 23, 2018
Messages
9
Hi all

I am new to VBA/Excel so I apologise in advance for my ignorance

I am trying to insert code that will count number of cells with a fill colour, I found the code below which works once but if you alter the number of cells with fill colour it does not automatically update the count displayed in the designated cell.
I also added: =colorfunction(G1,A1:B10,FALSE) in the cell I want the count displayed
I have filled G1 with yellow to use as reference cell

what i would like is this: when any cell in the range A2:B300 has a yellow fill then the count of such cells is displayed in G2

Any tips would be appreciated

Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi
welcome to forum

try this update to your code & see if does what you want

Code:
Function ColorFunction(ByVal rColor As Range, ByVal rRange As Range, Optional SUM As Boolean) As Long
    Dim rCell As Range
    Dim lCol As Long, vResult as Long
    
'A volatile function recalculates whenever
'calculation occurs in any cell on the worksheet
    Application.Volatile
    
    lCol = rColor.Interior.ColorIndex
    For Each rCell In rRange
        If rCell.Interior.ColorIndex = lCol Then
            vResult = IIf(SUM, WorksheetFunction.SUM(rCell, vResult), 1 + vResult)
        End If
    Next rCell


    ColorFunction = vResult
End Function

Dave
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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