获取有条件格式的单元格的背景颜色。

huangapple go评论75阅读模式
英文:

Get cell color of conditionally formatted cell

问题

我正在尝试使用VBA脚本获取单元格颜色

	Public Function GetCellColor(cell As Range) As Long
		Application.Volatile
		GetCellColor = cell.Interior.Color
	End Function

这适用于手动格式化的单元格。但如果我使用条件格式化的单元格,它只显示单元格的值,没有格式。

所以当我尝试使用下面的VBA,它应该找到条件格式化的单元格。但它给我一个“#VALUE!”错误。在下面的脚本中我做错了什么?

	Function GetCellColor(cell As Range) As String
		Application.Volatile
		GetCellColor = cell.DisplayFormat.Interior.Color
	End Function
英文:

I'm trying to get the cell color using the VBA script

Public Function GetCellColor(cell As Range) As Long
	Application.Volatile
	GetCellColor = cell.Interior.Color
End Function

This works for manually formatted cells. But if I use a conditionally formatted cell it just shows the value of the cell with no format.

So When I try to use this VBA below which supposed to find conditionally formatted cells. But it gives me a "#VALUE!" error. What am I doing wrong, on the script below?

Function GetCellColor(cell As Range) As String
	Application.Volatile
	GetCellColor = cell.DisplayFormat.Interior.Color
End Function

答案1

得分: 2

以下是一个用户自定义函数(UDF),它将为您提供单元格填充颜色(包括条件格式中的填充颜色),而不会出现您提到的“#VALUE错误”。

Option Explicit

Public Function CellFillColor(target As Range, Optional returnFormat As String = "IDX") As Variant
Dim retArray()
Dim rowCounter As Long
Dim colCounter As Long
Dim colorValue As Long
'    Application.Volatile
    If TypeName(target) = "Range" Then
        ReDim retArray(target.Rows.Count - 1, target.Columns.Count - 1)
        For rowCounter = 0 To target.Rows.Count - 1
            For colCounter = 0 To target.Columns.Count - 1
                colorValue = Evaluate("useDF(" & target.Cells(rowCounter + 1, colCounter + 1).Address & ")")
                Select Case UCase(returnFormat)
                    Case "RGB":
                                retArray(rowCounter, colCounter) = _
                                                                    Format((colorValue Mod 256), "00") & ", " & _
                                                                    Format(((colorValue \ 256) Mod 256), "00") & ", " & _
                                                                    Format((colorValue \ 65536), "00")
                    Case "HEX":
                                retArray(rowCounter, colCounter) = _
                                                                    "#" & _
                                                                    Format(Hex(colorValue Mod 256), "00") & _
                                                                    Format(Hex((colorValue \ 256) Mod 256), "00") & _
                                                                    Format(Hex((colorValue \ 65536)), "00")
                    Case "IDX": retArray(rowCounter, colCounter) = colorValue
                    Case Else: retArray(rowCounter, colCounter) = colorValue
                End Select
            Next colCounter
        Next rowCounter
        CellFillColor = retArray 'IIf(target.CountLarge = 1, retArray(0, 0), retArray)
    End If
End Function

Private Function useDF(ByVal target As Range) As Variant
    useDF = target.DisplayFormat.Interior.Color
End Function

'in Immediate Window
'Range("G16").Interior.Color=13551615<-IDX value

您还可以在我的 GitHub 上找到这个函数。

这是基于Jaafar Tribak的代码编写的,他在 mrexcel 上分享了这段代码。

希望这可以帮助您。随意拆解或重新排列它,因为上面的代码是为了给用户更多选择而编写的,因此可能显得(不必要地)较长。

英文:

Below is a UDF that will give you cell fill color <b>(including the fill color from Conditional Formatting)</b> without that <b><i>#VALUE error</i></b> that you mentioned.

Option Explicit

Public Function CellFillColor(target As Range, Optional returnFormat As String = &quot;IDX&quot;) As Variant
Dim retArray()
Dim rowCounter As Long
Dim colCounter As Long
Dim colorValue As Long
&#39;    Application.Volatile
    If TypeName(target) = &quot;Range&quot; Then
        ReDim retArray(target.Rows.Count - 1, target.Columns.Count - 1)
        For rowCounter = 0 To target.Rows.Count - 1
            For colCounter = 0 To target.Columns.Count - 1
                colorValue = Evaluate(&quot;useDF(&quot; &amp; target.Cells(rowCounter + 1, colCounter + 1).Address &amp; &quot;)&quot;)
                Select Case UCase(returnFormat)
                    Case &quot;RGB&quot;:
                                retArray(rowCounter, colCounter) = _
                                                                    Format((colorValue Mod 256), &quot;00&quot;) &amp; &quot;, &quot; &amp; _
                                                                    Format(((colorValue \ 256) Mod 256), &quot;00&quot;) &amp; &quot;, &quot; &amp; _
                                                                    Format((colorValue \ 65536), &quot;00&quot;)
                    Case &quot;HEX&quot;:
                                retArray(rowCounter, colCounter) = _
                                                                    &quot;#&quot; &amp; _
                                                                    Format(Hex(colorValue Mod 256), &quot;00&quot;) &amp; _
                                                                    Format(Hex((colorValue \ 256) Mod 256), &quot;00&quot;) &amp; _
                                                                    Format(Hex((colorValue \ 65536)), &quot;00&quot;)
                    Case &quot;IDX&quot;: retArray(rowCounter, colCounter) = colorValue
                    Case Else: retArray(rowCounter, colCounter) = colorValue
                End Select
            Next colCounter
        Next rowCounter
        CellFillColor = retArray &#39;IIf(target.CountLarge = 1, retArray(0, 0), retArray)
    End If
End Function

Private Function useDF(ByVal target As Range) As Variant
    useDF = target.DisplayFormat.Interior.Color
End Function

&#39;in Immediate Window
&#39;Range(&quot;G16&quot;).Interior.Color=13551615&lt;-IDX value

It can also be found on my GitHub.

It was based on <b>Jaafar Tribak</b>'s code that he shared on mrexcel.

I hope this helps.
Feel free to take it apart and/or rearrange it as above code was written for giving more choices to the user thereby making it (unnecessarily?) longer.

答案2

得分: 2

以下是代码部分的中文翻译:

返回条件格式颜色与UDF
-

- 以下的解决方案基于这个问题的第一个回答**Nay Lynn**提供感谢分享

[![输入图像描述][1]][1]

&lt;!-- 语言: lang-vb --&gt;

    Function GetColorCf(ByVal RangeCF As Range) As Variant
        
        Application.Volatile
        
        Dim ws As Worksheet: Set ws = RangeCF.Worksheet
            
        Dim rCount As Long: rCount = RangeCF.Rows.Count
        Dim cCount As Long: cCount = RangeCF.Columns.Count
        
        Dim Data(): ReDim Data(1 To rCount, 1 To cCount)
        
        Dim r As Long, c As Long, ColorValue As Long
        
        For r = 1 To rCount
            For c = 1 To cCount
                Data(r, c) = ws.Evaluate("GetCellColorCfVba(" _
                    & RangeCF.Cells(r, c).Address & ")")
            Next c
        Next r
        
        GetColorCf = Data
    
    End Function
    
    Private Function GetCellColorCfVba(ByVal cell As Range) As Long
        With cell.Cells(1) ' 确保单个单元格
            Dim ColorValue As Long: ColorValue = .DisplayFormat.Interior.Color
            If ColorValue = 16777215 Then
                If .DisplayFormat.Interior.ColorIndex = xlNone Then
                    ColorValue = 0
                End If
            End If
        End With
        GetCellColorCfVba = ColorValue
    End Function

请注意,这是代码的中文翻译部分,不包括问题的其他内容。如果您有其他需要,请随时提问。

英文:

Return Conditional Formatting Colors With a UDF

  • The following workaround is based on the first answer to this question by Nay Lynn. Thanks for sharing.

获取有条件格式的单元格的背景颜色。

<!-- language: lang-vb -->

Function GetColorCf(ByVal RangeCF As Range) As Variant
    
    Application.Volatile
    
    Dim ws As Worksheet: Set ws = RangeCF.Worksheet
        
    Dim rCount As Long: rCount = RangeCF.Rows.Count
    Dim cCount As Long: cCount = RangeCF.Columns.Count
    
    Dim Data(): ReDim Data(1 To rCount, 1 To cCount)
    
    Dim r As Long, c As Long, ColorValue As Long
    
    For r = 1 To rCount
        For c = 1 To cCount
            Data(r, c) = ws.Evaluate(&quot;GetCellColorCfVba(&quot; _
                &amp; RangeCF.Cells(r, c).Address &amp; &quot;)&quot;)
        Next c
    Next r
    
    GetColorCf = Data

End Function

Private Function GetCellColorCfVba(ByVal cell As Range) As Long
    With cell.Cells(1) &#39; ensure single cell
        Dim ColorValue As Long: ColorValue = .DisplayFormat.Interior.Color
        If ColorValue = 16777215 Then
            If .DisplayFormat.Interior.ColorIndex = xlNone Then
                ColorValue = 0
            End If
        End If
    End With
    GetCellColorCfVba = ColorValue
End Function

huangapple
  • 本文由 发表于 2023年6月13日 09:39:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定