は2つの引数を渡すことができる機能、セルの範囲及びカラーインデックス、色のための標準的な名前、または1つの細胞のいずれかとすることができる変異体であります範囲。それはその引数の範囲を渡された場合、それは範囲のカラーインデックスに対して合計:
Function IndexFromName(ByVal colorName As String) As Long:
Dim s As String, i As Long
s = LCase(colorName)
Select Case s
Case "black": i = 1
Case "white": i = 2
Case "red": i = 3
Case "green": i = 4
Case "blue": i = 5
Case "yellow": i = 6
Case "magenta": i = 7
Case "cyan": i = 8
Case Else: i = -4142 'color automatic -- perhaps should return an error
End Select
IndexFromName = i
End Function
Function SumIfColor(R As Range, color As Variant) As Double
'summ cells with a given color index
'which may differ from displayed color
'if conditional formatting is in use!
Dim i As Long, ci As Long, s As Double, c As Range
If TypeName(color) = "Range" Then
ci = color.Interior.ColorIndex
ElseIf TypeName(color) = "String" Then
ci = IndexFromName(color)
Else
ci = color
End If
For Each c In R.Cells
If c.Interior.ColorIndex = ci Then s = s + c.Value
Next c
SumIfColor = s
End Function
これは、スプレッドシートに直接使用することができる:上記画像Iにおいて
セルA14に式=SumIfColor($A$1:$H$10,A13)
を入力し、nxtの2つの列にコピーしました。
もちろん、IndexFromName
を展開することもできます。私はvbYellow
のような名前付き定数に対応する8色しか実装していませんでした。たとえば、標準のExcelカラーパレットで他の色の標準HTML色名を決定することができます。
Excelの色は、セルのカラーインデックスよりもかなり複雑になっています。表示された色はもはやパレットに限定されず、条件付き書式化は一定の複雑な要因である。唯一の有効なユースケースは、表示される色のみが、ユーザーがカラーパレットを使用して明示的に選択した色である場合です。