I have a worksheet that has cells coloured via conditional formatting. I need to count the various cells by colour - after much searching & experimenting I've got the two functions below to work with the following formula :-
=CountCellsByColor(range, color code)
Problem is that I need to do this in many different workbooks. All my other macros are housed in a central workbook so that I don't have to do much more than open the "storage" workbook, the file to be worked on & then run the appropriate macro.
My question is how do I use the two functions below via that central workbook ??
What changes do I need to make to the functions (if any) & how would I "execute" the formula via the macro or would it be as simple as Range("A1").formula="=CountCellsByColor(range, color code)" ??
Many thanks
Function GetCellColor(xlRange As Range)
Dim indRow, indColumn As Long
Dim arResults()
Application.Volatile
If xlRange Is Nothing Then
Set xlRange = Application.ThisCell
End If
If xlRange.Count > 1 Then
ReDim arResults(1 To xlRange.Rows.Count, 1 To xlRange.Columns.Count)
For indRow = 1 To xlRange.Rows.Count
For indColumn = 1 To xlRange.Columns.Count
arResults(indRow, indColumn) = xlRange(indRow, indColumn).Interior.Color
Next
Next
GetCellColor = arResults
Else
GetCellColor = xlRange.Interior.Color
End If
End Function
Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
Dim indRefColor As Long
Dim cellCurrent As Range
Dim cntRes As Long
Application.Volatile
cntRes = 0
indRefColor = cellRefColor.Cells(1, 1).Interior.Color
For Each cellCurrent In rData
If indRefColor = cellCurrent.Interior.Color Then
cntRes = cntRes + 1
End If
Next cellCurrent
CountCellsByColor = cntRes
End Function
Bookmarks