G
gwag17
I am trying to write a function that will calculate the sum of cells i
a range depending on their background color.
I have looked on the net and found some sample code, tried it and i
doesnt work.
the code is as follows:
Function colorfunction(rcolor As Range, rRange As Range, Optional SU
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 = 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
The count part of this works, however if i try the SUM, it gives me
#VALUE!
Any help on this would be great.
Cheer
a range depending on their background color.
I have looked on the net and found some sample code, tried it and i
doesnt work.
the code is as follows:
Function colorfunction(rcolor As Range, rRange As Range, Optional SU
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 = 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
The count part of this works, however if i try the SUM, it gives me
#VALUE!
Any help on this would be great.
Cheer