excel 获取条件格式单元格的单元格颜色

7gyucuyw  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(264)

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

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

这适用于手动格式化的单元格。但是如果我使用一个有条件格式化的单元格,它只显示单元格的值,没有格式。
所以当我尝试使用下面的VBA来查找条件格式的单元格时。但它给了我一个“#值!"错误。我在下面的脚本中做错了什么?

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

6l7fqoea1#

下面是一个UDF,它将为您提供单元格填充颜色**(包括条件格式中的填充颜色),而没有您提到的#VALUE error**。

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 Tribakmrexcel上分享的代码。
希望这能帮上忙。
随意拆开和/或重新排列它,因为上面的代码是为了给用户更多的选择而编写的,从而使它(不必要的?))更长。

ubby3x7f

ubby3x7f2#

返回带自定义项的条件格式化颜色

  • 以下解决方法基于Nay林恩对此问题的第一个答案。谢谢分享。

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) ' 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

相关问题