![](/img/trans.png)
[英]How do I retrieve an Excel cell value in VBA as formatted in the WorkSheet?
[英]How do I find the fill colour value of a conditionally formatted cell in Excel 2007 using vba?
我在Excel 2007中使用顏色比例進行條件格式化,我很難找到條件格式化單元格的填充顏色代碼。 我知道Interior.Color返回默認顏色值,但在使用條件格式時沒有用。 我很中庸地意識到這有多么艱難。
謝謝。
您可以訪問fomatting條件的內部顏色(而不是單元格當前的顏色),假設這是應用於單元格的第一個條件:
Range("A1").FormatConditions(1).interior.color
這是一個函數,它將返回單元格包含的所有條件格式的顏色代碼。 如果沒有條件,它將不返回任何內容,如果有條件但沒有設置顏色,則它會告訴您“無”。
Function ConditionalColor(ByVal cell As Range)
Dim colors As String
Dim i As Long
For i = 1 To Range(cell.Address).FormatConditions.count
If Range(cell.Address).FormatConditions(i).Interior.Color <> 0 Then
colors = colors & "Condition " & i & ": " & _
Range(cell.Address).FormatConditions(i).Interior.Color & vbLf
Else
colors = colors & "Condition " & i & ": None" & vbLf
End If
Next
If Len(colors) <> 0 Then
colors = Left(colors, Len(colors) - 1)
End If
ConditionalColor = colors
End Function
更新 :如果你好奇(我是),Excel使用的顏色代碼實際上是BGR,而不是RGB。 因此,如果您想將代碼轉換為RGB值,可以使用:
Function GetRGB(ByVal cell As range) As String
Dim R As String, G As String
Dim B As String, hexColor As String
hexCode = Hex(cell.Interior.Color)
'Note the order excel uses for hex is BGR.
B = Val("&H" & Mid(hexCode, 1, 2))
G = Val("&H" & Mid(hexCode, 3, 2))
R = Val("&H" & Mid(hexCode, 5, 2))
GetRGB = R & ":" & G & ":" & B
End Function
嗨您提供的答案無效,因為我使用的是色標,因此它不會返回正常的3個條件值。
經過更多的搜索,我找到了一個有效的解決方法。 那就是取數據並將其放入單詞然后將其復制回excel,使得范圍在單元格中變為真彩色,因此Interior.Color將起作用。 我找到了一個已經把它帶入VBA的人。 如果其他人想要這樣做,這里是它的鏈接 。
我沒有適用於Excel 2007或更低版本的答案,但從Excel 2010開始,您可以使用以下內容(更改范圍以適應):
Range("A1").DisplayFormat.Interior.ColorIndex
幸運的是,雖然Excel 2003以后支持我需要的軟件,但實際上我只是在測試過程中需要它,並且測試模塊已從生產版本中刪除。
下面的代碼來自VBAExpress,所有信用都是原作者 - byundt。
可能需要針對excel 2007進行修改。
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
簡單方法:打印電子表格屏幕。 將其粘貼到油漆中。 使用移液器工具查找顏色。 單擊編輯顏色。
BOOM找到了您可以輸入到Excel中的RGB信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.