簡體   English   中英

Excel 2007條件格式 - 如何獲取單元格顏色?

[英]Excel 2007 conditional formatting - how to get cell color?

假設我有以下范圍(a1:c3)

  A B C
1 -1 1 1
2 -1 0 0
3  0 0 1

現在我選擇了以下范圍,並使用條件格式(使用默認的紅黃綠色標度)對其進行格式化....現在范圍顏色變為

    A         B         C
1 Green    Red     Red
2 Green   Yellow Yellow
3 Yellow Yellow Red

現在我想詢問范圍內任何單元格的顏色,例如MsgBox Range(“A1”)。Interior.Color但它不是說它是綠色,為什么? 你能幫助我嗎?

范圍(“A1”)。Interior.Color始終返回16777215范圍(“A1”)。Interior.ColorIndex始終返回-4142

(無論A1的顏色是紅色,藍色,綠色......)

范圍(“A1”,“C3”)。FormatConditions.Count這一個總是返回0,為什么?

.Interior.Color返回“真實”顏色,而不是有條件格式的顏色結果。

@sss:它不能通過API獲得。

您可以做的最好的方法是測試條件格式中使用的相同條件。

為避免這種情況導致代碼重復,我建議將條件標准移至UDF。 例子:

Function IsGroup1(ByVal testvalue As Variant) As Boolean
   IsGroup1 = (testvalue < 0)
End Function

Function IsGroup2(ByVal testvalue As Variant) As Boolean
   IsGroup1 = (testvalue = 0)
End Function

Function IsGroup3(ByVal testvalue As Variant) As Boolean
   IsGroup1 = (testvalue > 0)
End Function

然后在條件格式中使用這些公式:

=IsGroup1(A1)
=IsGroup2(A1)
=IsGroup3(A1)

然后你的代碼,而不是查看單元格的顏色 ,看看是否滿足條件:

If IsGroup1(Range("$A$1").Value) Then MsgBox "I'm red!"

您需要引用<Cell>.FormatConditions(index that is active).Interior.ColorIndex來檢索單元格的條件格式顏色。

您可以參考以下鏈接獲取示例:

http://www.xldynamic.com/source/xld.CFConditions.html#specific

作為@richardtallent的后續(抱歉,我無法做評論),以下鏈接將為您提供一個函數,通過為您評估條件格式來返回顏色索引。

http://www.bettersolutions.com/excel/EPX299/LI041931911.htm

因為我可能在一段時間內有三種以上的不同顏色......我沒有找到任何使用條件格式的默認顏色來處理它的好方法......我這樣做了。 然后每當我問到細胞的顏色時,我都會找回正確的顏色!

 for (int t = 0; t < d_distinct.Length; t++ )
 {                        
   Excel.FormatCondition cond =
    (Excel.FormatCondition)range.FormatConditions.Add(
    Excel.XlFormatConditionType.xlCellValue,
    Excel.XlFormatConditionOperator.xlEqual, 
    "="+d_distinct[t],
    mis, mis, mis, mis, mis);
   cond.Interior.PatternColorIndex = 
    Excel.Constants.xlAutomatic;
  cond.Interior.TintAndShade = 0;
  cond.Interior.Color = ColorTranslator.ToWin32(c[t]);
  cond.StopIfTrue = false;                        
}

d_distinct包含范圍內的所有不同值... c是一個Color [],它為每個不同的值保存不同的顏色! 這段代碼很容易翻譯成vb!

根據XlColorIndex Enumeration ColorIndex=-4142表示無顏色

至於為什么會發生這種情況我很無能為力。 返回值似乎是RGB值的十進制表示。 此腳本的改進版本將值解密為十六進制RGB表示法

Function RGB(CellRef As Variant)
   RGB = ToHex(Range(CellRef).Interior.Color)
End Function

Function ToHex(ByVal N As Long) As String
   strH = ""
   For i = 1 To 6
      d = N Mod 16
      strH = Chr(48 + (d Mod 9) + 16 * (d \ 9)) & strH
      N = N \ 16
   Next i
   strH2 = ""
   strH2 = Right$(strH, 2) & Mid$(strH, 3, 2) & Left$(strH, 2)
   ToHex = strH2
End Function

要獲取范圍中單元格的顏色,需要以范圍(“A1”,“C3”)的形式引用數組內的單個單元格。單元格(1,1)(對於單元格A1)。 如果您查找遇到問題的屬性的名稱,Excel幫助非常好。

此外,Excel 2007使用Integers作為其顏色類型,因此最好的辦法是將顏色索引指定為整數,並在整個程序中使用它。 對於您的示例,請嘗試:

Green = Range("A1","C3").Cells(1,1).Interior.Color
Yellow = Range("A1","C3").Cells(1,3).Interior.Color
Red = Range("A1","C3").Cells(2,1).Interior.Color

然后將顏色切換為全紅色:

Range("A1","C3").Interior.Color = Red

再次,檢查Excel幫助以了解如何使用單元格([RowIndex],[ColumnIndex])。

如果以上內容對您不起作用,請檢查.Interior.PatternColorIndex等於。 我通常將它設置為xlAutomatic(純色),如果顏色沒有變化,它可以設置為其他東西。

似乎沒有“條件格式”-color以編程方式可用。 相反,我建議您編寫一個計算單元格顏色的小函數,然后只要設置一個宏,就可以在編輯該值時在活動單元格上運行它。 例如(抱歉偽代碼 - 我不再是VBA專家了):

Function GetColorForThisCell(Optional WhatCell as String) as Int

   If WhatCell="" Then WhatCell = ActiveCell

   If Range(WhatCell).value = -1 then GetColorForThisCell = vbGreen
   If Range(WhatCell).value =  0 then GetColorForThisCell = vbYellow
   If Range(WhatCell).value =  1 then GetColorForThisCell = vbRed
End Function

Sub JustEditedCell
   ActiveCell.color = GetColorForThisCell()
End Sub

Sub GetColorOfACell(WhatCell as string)
   Msgbox(GetColorForThisCell(WhatCell) )
End Sub

雖然您無法使用內置的Excel條件格式,但這可以完成同樣的事情,並且您可以從代碼中讀取顏色。 這有意義嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM