ancient
10-30-2016, 03:27 AM
Hi All
I have found a couple of different Vba methods for count by colour, but would like clarification please:
1st method:
Function SumByColor(CellColor As Range, rRange As Range)
Application.Volatile
Dim cSum As Single
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
The original value in line 3 was "Dim cSum As long" but it would then round up/down the results, which I did not want, so "Dim cSum As single" seems to work...is this correct?
I can also replace it with "Dim TCell As Range" this also seems to work
Can someone please explain what the actual differences are?
2nd Method
Function SumByColor(CellColor As Range, SumRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In SumRange
If ICol = TCell.Interior.ColorIndex Then
SumByColor = SumByColor + TCell.Value
End If
Next TCell
End FunctionAgain could someone please confirm if both methods provide accurate results...?
Is one method better than the other?
What are the actual differences?
with the addition of volatile, when the colours are changed and the sheet is saved/closed the figures are updated....is there another way (apart from a calculate all macro)
Thank you in advance for any insight.
Kind Regards
I have found a couple of different Vba methods for count by colour, but would like clarification please:
1st method:
Function SumByColor(CellColor As Range, rRange As Range)
Application.Volatile
Dim cSum As Single
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
The original value in line 3 was "Dim cSum As long" but it would then round up/down the results, which I did not want, so "Dim cSum As single" seems to work...is this correct?
I can also replace it with "Dim TCell As Range" this also seems to work
Can someone please explain what the actual differences are?
2nd Method
Function SumByColor(CellColor As Range, SumRange As Range)
Application.Volatile
Dim ICol As Integer
Dim TCell As Range
ICol = CellColor.Interior.ColorIndex
For Each TCell In SumRange
If ICol = TCell.Interior.ColorIndex Then
SumByColor = SumByColor + TCell.Value
End If
Next TCell
End FunctionAgain could someone please confirm if both methods provide accurate results...?
Is one method better than the other?
What are the actual differences?
with the addition of volatile, when the colours are changed and the sheet is saved/closed the figures are updated....is there another way (apart from a calculate all macro)
Thank you in advance for any insight.
Kind Regards