Return RGB values from Range.Interior.Color (or any other Color Property)

前端 未结 7 1411
无人共我
无人共我 2020-11-27 06:55

I was trying to incrementally change the background color of a cell to black, and I found that the Range.Interior.Color method returns a Long which is seemingly arbitrary.

7条回答
  •  误落风尘
    2020-11-27 07:35

    Mark Balhoff´s VBA script works fine. All credits go to him.

    In case you´d like to get the color codes/indexes of conditionally formatted cells as well, the code may be amended like this:

    '----------------------------------------------------------------
    '   Function            Color
    '   Purpose             Determine the Background Color Of a Cell
    '   @Param rng          Range to Determine Background Color of
    '   @Param formatType   Default Value = 0
    '                       0   Integer             color of cell, not considering conditional formatting color
    '                       1   Hex                 color of cell, not considering conditional formatting color
    '                       2   RGB                 color of cell, not considering conditional formatting color
    '                       3   Excel Color Index   color of cell, not considering conditional formatting color
    '                       4   Integer             "real" visible color of cell (as the case may be the conditional formatting color)
    '                       5   Hex                 "real" visible color of cell (as the case may be the conditional formatting color)
    '                       6   RGB                 "real" visible color of cell (as the case may be the conditional formatting color)
    '                       7   Excel Color Index   "real" visible color of cell (as the case may be the conditional formatting color)
    '   Usage               Color(A1)      -->   9507341
    '                       Color(A1, 0)   -->   9507341
    '                       Color(A1, 1)   -->   91120D
    '                       Color(A1, 2)   -->   13, 18, 145
    '                       Color(A1, 3)   -->   6
    '-----------------------------------------------------------------
    Function Color(rng As Range, Optional formatType As Integer = 0) As Variant
        Dim colorVal As Variant
        Select Case formatType
            Case 0 To 3
                colorVal = Cells(rng.Row, rng.Column).Interior.Color
            Case 4 To 7
                colorVal = Cells(rng.Row, rng.Column).DisplayFormat.Interior.Color
        End Select
        Select Case formatType
            Case 0
                Color = colorVal
            Case 1
                Color = Hex(colorVal)
            Case 2
                Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
            Case 3
                Color = Cells(rng.Row, rng.Column).Interior.ColorIndex
            Case 4
                Color = colorVal
            Case 5
                Color = Hex(colorVal)
            Case 6
                Color = (colorVal Mod 256) & ", " & ((colorVal \ 256) Mod 256) & ", " & (colorVal \ 65536)
            Case 7
                Color = Cells(rng.Row, rng.Column).DisplayFormat.Interior.ColorIndex
        End Select
    End Function
    

提交回复
热议问题