Sum Visible Highlighted Cells In Column VBA

fiberboysa

Board Regular
Joined
Apr 25, 2012
Messages
106
Office Version
  1. 365
Platform
  1. Windows
Hi All,
I have a sheet in which I need to sum values based on condition that these cells need to be highlighted/filled with a color and it should be visible in filter.
I found following code which is working for Highlighted/filled cells but it is summing up all visible and hidden cells...

VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
 If cl.Interior.ColorIndex = ColIndex Then
 cSum = WorksheetFunction.Sum(cl, cSum)
 End If
Next cl
SumByColor = cSum
End Function

Please can anyone help me to modify this code or give me a new code to sum Highlighted Visible cells only?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Untested, but this should exclude hidden, filled cells.
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
    If Not cl.EntireRow.Hidden Then
        If cl.Interior.ColorIndex = ColIndex Then
            cSum = WorksheetFunction.Sum(cl, cSum)
        End If
    End If
Next cl
SumByColor = cSum
End Function
 
Upvote 0
Solution
Untested, but this should exclude hidden, filled cells.
VBA Code:
Function SumByColor(CellColor As Range, rRange As Range)
Dim cSum As Long
Dim ColIndex As Integer
ColIndex = CellColor.Interior.ColorIndex
For Each cl In rRange
    If Not cl.EntireRow.Hidden Then
        If cl.Interior.ColorIndex = ColIndex Then
            cSum = WorksheetFunction.Sum(cl, cSum)
        End If
    End If
Next cl
SumByColor = cSum
End Function
Working perfectly. Thanks dear. Be blessed :)
 
Upvote 0

Forum statistics

Threads
1,215,034
Messages
6,122,782
Members
449,095
Latest member
m_smith_solihull

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