Count or Sum of colored cells

RAMU

Active Member
Joined
Dec 11, 2009
Messages
345
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
  2. Mobile
Dear Friends,

Refer to my title I have been using UDF from the following link:
http://www.ozgrid.com/VBA/Sum.htm

But excel can't automatically change the value if I modify any cell color.

Is there any updated code using which values will be changed automatically ?

Any help will be highly appreciated.


Regards
RAMU
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Dear Mr. Hobson,

Thnx for your help but can't get you. Can you pls explain the process?

Regards
RAMU
 
Upvote 0
The selection event fires at the beginning so the best bet is to make the function volatile. This means that when any recalculation is done, it will fire.

I will list the selection event after the example. Right click the Sheet's tab, View > Code, and paste. To use it after you change the cell rColor, reselect it to fire it. You can add some other ranges to trigger the code in the Intersect().

Code:
Function SumColor(rColor As Range, rSumRange As Range)

Application.Volatile
''''''''''''''''''''''''''''''''''''''

'Written by Ozgrid Business Applications

'www.ozgrid.com



'Sums cells based on a specified fill color.

'''''''''''''''''''''''''''''''''''''''

  Dim rCell As Range

  Dim iCol As Integer

  Dim vResult



  iCol = rColor.Interior.ColorIndex



    For Each rCell In rSumRange

      If rCell.Interior.ColorIndex = iCol Then

        vResult = WorksheetFunction.Sum(rCell) + vResult

      End If

    Next rCell



  SumColor = vResult

End Function

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim iRange As Range
  Set iRange = Intersect(Target, Range("B1"))
  If iRange Is Nothing Then Exit Sub
  Application.Calculate
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

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