如果值和颜色计数
Function CIVAC(Range As Range, Value As Variant, _
Optional ColorIndex As Long = -4142, _
Optional Compare As Integer = 1) As Long
'Title
'Count If Value And Color
'Description
'In a specified contiguous range, counts the number of cells both,
'containing a specified value and having a specified Interior ColorIndex.
Dim arrVal As Variant 'Range Array
Dim arrClr() As Long 'ColorIndex Array
Dim lngVal As Long 'Row Counter
Dim iVal As Integer 'Column Counter
Dim lngResult As Long 'Result Accumulator
'Values
arrVal = Range.Areas(1) 'Prevent Multiple Areas Error
'ColorIndexes
ReDim arrClr(LBound(arrVal) To UBound(arrVal), _
LBound(arrVal, 2) To UBound(arrVal, 2))
For lngVal = LBound(arrClr) To UBound(arrClr)
For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
arrClr(lngVal, iVal) = Range.Cells(lngVal, iVal).Interior.ColorIndex
Next
Next
'Count
For lngVal = LBound(arrClr) To UBound(arrClr)
For iVal = LBound(arrClr, 2) To UBound(arrClr, 2)
If Not IsError(arrVal(lngVal, iVal)) Then 'Prevent VBA Errors
If InStr(1, arrVal(lngVal, iVal), Value, Compare) <> 0 And _
arrClr(lngVal, iVal) = ColorIndex Then lngResult = lngResult + 1
End If
Next
Next
CIVAC = lngResult
End Function
很好,但是这个单元格中颜色的“内部颜色索引”是多少?
细胞内部颜色指数
Function CICI(CellRange As Range) As Long
'Title
'Cell Interior Color Index
'Description
'Returns the Interior ColorIndex of a specified cell ('CellRange').
'If 'CellRange' contains more than one cell, it uses the first cell.
CICI = CellRange(1, 1).Interior.ColorIndex
End Function