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
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