3

以下のコードを使用して、Excelでセルのカラーインデックスを取得しました。

元のリンク

Function ConditionalColor(rg As Range, FormatType As String) As Long
  'Returns the color index (either font or interior) of the first cell in range rg. If no _
 conditional format conditions apply, Then returns the regular color of the cell. _
    FormatType Is either "Font" Or "Interior"
    Dim cel As Range
    Dim tmp As Variant
    Dim boo As Boolean
    Dim frmla As String, frmlaR1C1 As String, frmlaA1 As String
    Dim i As Long

     'Application.Volatile    'This statement required if Conditional Formatting for rg is determined by the _
    value of other cells

    Set cel = rg.Cells(1, 1)
    Select Case Left(LCase(FormatType), 1)
    Case "f" 'Font color
        ConditionalColor = cel.Font.ColorIndex
    Case Else 'Interior or highlight color
        ConditionalColor = cel.Interior.ColorIndex
    End Select

    If cel.FormatConditions.Count > 0 Then
         'On Error Resume Next
        With cel.FormatConditions
            For i = 1 To .Count 'Loop through the three possible format conditions for each cell
                frmla = .Item(i).Formula1
                If Left(frmla, 1) = "=" Then 'If "Formula Is", then evaluate if it is True
                     'Conditional Formatting is interpreted relative to the active cell. _
                    This cause the wrong results If the formula isn 't restated relative to the cell containing the _
                    Conditional Formatting--hence the workaround using ConvertFormula twice In a row. _
                    If the Function were Not called using a worksheet formula, you could just activate the cell instead.
                    frmlaR1C1 = Application.ConvertFormula(frmla, xlA1, xlR1C1, , ActiveCell)
                    frmlaA1 = Application.ConvertFormula(frmlaR1C1, xlR1C1, xlA1, xlAbsolute, cel)
                    boo = Application.Evaluate(frmlaA1)
                Else 'If "Value Is", then identify the type of comparison operator and build comparison formula
                    Select Case .Item(i).Operator
                    Case xlEqual ' = x
                        frmla = cel & "=" & .Item(i).Formula1
                    Case xlNotEqual ' <> x
                        frmla = cel & "<>" & .Item(i).Formula1
                    Case xlBetween 'x <= cel <= y
                        frmla = "AND(" & .Item(i).Formula1 & "<=" & cel & "," & cel & "<=" & .Item(i).Formula2 & ")"
                    Case xlNotBetween 'x > cel or cel > y
                        frmla = "OR(" & .Item(i).Formula1 & ">" & cel & "," & cel & ">" & .Item(i).Formula2 & ")"
                    Case xlLess ' < x
                        frmla = cel & "<" & .Item(i).Formula1
                    Case xlLessEqual ' <= x
                        frmla = cel & "<=" & .Item(i).Formula1
                    Case xlGreater ' > x
                        frmla = cel & ">" & .Item(i).Formula1
                    Case xlGreaterEqual ' >= x
                        frmla = cel & ">=" & .Item(i).Formula1
                    End Select
                    boo = Application.Evaluate(frmla) 'Evaluate the "Value Is" comparison formula
                End If

                If boo Then 'If this Format Condition is satisfied
                    On Error Resume Next
                    Select Case Left(LCase(FormatType), 1)
                    Case "f" 'Font color
                        tmp = .Item(i).Font.ColorIndex
                    Case Else 'Interior or highlight color
                        tmp = .Item(i).Interior.ColorIndex
                    End Select
                    If Err = 0 Then ConditionalColor = tmp
                    Err.Clear
                    On Error GoTo 0
                    Exit For 'Since Format Condition is satisfied, exit the inner loop
                End If
            Next i
        End With
    End If

End Function

ただし、以下に示すように、2つの異なる色のセルは、まったく同じカラーインデックスを提供します。

ここに画像の説明を入力してください

このエラーを修正する方法は?

ここにテストファイルを添付しました。このエラーを確認してください。

4

2 に答える 2

3

混乱の原因である可能性が非常に高いと思うのは、条件付き書式です。2 つのセルの元の ColorIndex または Color は同じです。ただし、条件付き書式は元の色を「上書き」します。次に、セルから ColorIndex または Color プロパティを取得しようとすると、結果は表示されるものではなく、元の/基になるものになります。

ところで、取得したいのがセルの ColorIndex だけである場合は、このコードのようなものを使用して、ここで説明したことがあなたのケースであるかどうかをテストできます。

MsgBox ActiveCell.Interior.ColorIndex

MsgBox ActiveCell.Interior.Color

または、右側の次のセルに書き込むには:

ActiveCell.Offset(0, 1).Value = ActiveCell.Interior.ColorIndex
于 2012-06-29T16:25:31.853 に答える
3

編集:私の以前の回答はあなたの問題を解決しませんが、同じ質問をしている誰かに関連している可能性があると思います.

あなたが見ている問題は、Color のようなより具体的なものではなく、Colorindex プロパティの使用に起因します。

この 2 つの間の完全な説明については、次のアドレスを参照してください: http://msdn.microsoft.com/en-us/library/cc296089(v=office.12).aspx

基本的に、可能なカラー インデックス値は 57 しかありませんが、はるかに多くの色を使用できます。カラー インデックスは、特定のパレット内のインデックスを参照します。同じインデックスを持つ 2 つの色をたまたま見つけました。プログラムが期待どおりに機能するようにするには、colorindex 参照を color に更新する必要があります。変更を行わないと、引き続き混乱する結果になります。


前の回答: 値を適用する必要があるセルを推測する条件付き書式を使用している場合、UDF が条件付き書式が true かどうかを確認するときに、通常は現在のセルに従います。

たとえば、条件付き書式の数式が次のような場合:

=MOD(ROW(),2)=1

コードがヒットするたびに:

frmlaR1C1 = Application.ConvertFormula(frmla, xlA1, xlR1C1, , ActiveCell)
frmlaA1 = Application.ConvertFormula(frmlaR1C1, xlR1C1, xlA1, xlAbsolute, cel)                      
boo = Application.Evaluate(frmlaA1) 

条件付き書式が適用されているセルではなく、現在アクティブなセルに基づいて評価されます。

少し実験を行いましたが、コードを使用する頻度によっては、数式を強化することが最善の結果になると思います。これですべての問題が解決するわけではありませんが、最初の ConvertFormula 呼び出しの直前に次を挿入してみてください。

frmla = Replace(frmla, "()", "(" & cel.Address & ")")

Row() または Column() を使用して解決します。

これで問題が完全に解決しない場合は、条件付き書式の数式を確認する必要があります。

于 2012-06-29T17:02:48.017 に答える