2012-06-29 242 views
2

我使用下面的代碼獲取Excel中單元格的顏色索引。Excel中的顏色不同但顏色指數相同VBA

Original Link

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種不同的顏色的細胞得到完全相同的顏色指數:

enter image description here

如何解決這個錯誤?我附上測試​​文件here。請檢查這個錯誤。

回答

2

編輯:我以前的答案沒有解決您的問題,但我認爲它可能仍然是相關的人詢問同樣的問題。

您看到的問題源於使用Colorindex屬性,而不是像顏色那樣更具體。

對於兩者之間的徹底的解釋,你可以參考這個地址: http://msdn.microsoft.com/en-us/library/cc296089(v=office.12).aspx

從本質上講,只有57可能的顏色索引值,但更可用的顏色。顏色索引是指給定調色板中的索引。偶然發現了兩種具有相同索引的顏色。要使程序正常工作,應該將colorindex引用更新爲顏色。沒有做出改變,你將繼續產生令人困惑的結果。


以前的答案: 如果您使用的是推斷其值應適用的電池條件格式,那麼當UDF檢查,以確定是否條件格式是真的,那通常會推遲到當前單元格。

舉例來說,如果你的條件格式的公式是這樣的:

= 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 & ")") 

解決了它使用的行(或列)。

如果這不能完全解決您的問題,我們需要查看您的條件格式公式。

+0

嗨丹尼爾庫克,我的公式單元格的例子是= ConditionalColor(A1,「內部」)或= ConditionalColor(A2,「內部」) – dakiquang

+0

嗨,我剛剛更新測試文件,請檢查它。謝謝:-) – dakiquang

+0

我不認爲@dakiquang使用'條件格式公式'丹尼爾 –

2

我認爲造成混淆的可能原因是條件格式。 兩個單元的原始ColorIndex或Color是相同的。但條件格式「覆蓋」原始顏色。然後,如果您嘗試將ColorIndex或Color屬性移出單元格,則結果不是您看到的,而是原始/底層的。

順便說一句,如果你想得到的是單元格的ColorIndex,你可以使用像這樣的代碼來測試我在這裏描述的是你的情況。

MsgBox ActiveCell.Interior.ColorIndex 

MsgBox ActiveCell.Interior.Color 

,或者使其到右側的下一個單元格寫:

ActiveCell.Offset(0, 1).Value = ActiveCell.Interior.ColorIndex 
+0

您好LeNoob,我試圖用作 功能CellColorIndex(INRANGE作爲範圍,可選OfText由於布爾)的基本代碼作爲整數 「 」該函數返回一個內政部 的ColorIndex值'的(背景)一個單元格,或者如果OfText爲真,則單元格中的字體。 「 如果OfText = TRUE,則 CellColorIndex = InRange.Font.ColorIndex 否則 CellColorIndex = InRange.Interior.ColorIndex 結束如果 端功能 ,但得到同樣的錯誤 – dakiquang

+0

您好,我剛剛更新的測試文件, 請檢查一下。謝謝:-) – dakiquang