Excel formula that prints cell color (ColorIndex or RGB)

后端 未结 3 834
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 12:30

Is there, in Excel, a Formula that retrieve the ColorIndex (or RGB) of a cell?

I found the follwing function:

CELL(info_type, the_cell)
3条回答
  •  失恋的感觉
    2020-12-18 12:47

    Here are some small functions for you. From your sheet, press Alt-F11 to reach the VBA editor, insert a new module, paste the below code, go back to your worksheet and use them by their names, like in =FillColor(A1)

    The first two are the promised "3-liners" giving decimal values for font and background colors - not very useful though

    The second pair converts the decimal number to RGB and returns a string of format N, N, N

    The third pair are array formulas - select 3 cells in a row, enter the formula and press Ctrl+Shift+Enter to obtain numeric RGB values in 3 neighboring cells

    Function FillColor(Target As Range) As Variant
        FillColor = Target.Interior.Color
    End Function
    
    Function FontColor(Target As Range) As Variant
        FontColor = Target.Font.Color
    End Function
    
    Function FillColorRGB(Target As Range) As Variant
    Dim N As Double
    
        N = Target.Interior.Color
        FillColorRGB = Str(N Mod 256) & ", " & Str(Int(N / 256) Mod 256) & ", " & Str(Int(N / 256 / 256) Mod 256)
    End Function
    
    Function FontColorRGB(Target As Range) As Variant
    Dim N As Double
    
        N = Target.Font.Color
        FontColorRGB = Str(N Mod 256) & ", " & Str(Int(N / 256) Mod 256) & ", " & Str(Int(N / 256 / 256) Mod 256)
    End Function
    
    Function FillColorRGBArray(Target As Range) As Variant
    Dim N As Double, A(3) As Integer
    
        N = Target.Interior.Color
        A(0) = N Mod 256
        A(1) = Int(N / 256) Mod 256
        A(2) = Int(N / 256 / 256) Mod 256
        FillColorRGBArray = A
    End Function
    
    Function FontColorRGBArray(Target As Range) As Variant
    Dim N As Double, A(3) As Integer
    
        N = Target.Font.Color
        A(0) = N Mod 256
        A(1) = Int(N / 256) Mod 256
        A(2) = Int(N / 256 / 256) Mod 256
        FontColorRGBArray = A
    End Function
    

    A word of caution: changing the color of a cell does not start recalculation by the above functions/formulas, as recoloring a cell in general is not supposed to drive recalculation. You have to manually start a full recalculation using Ctrl+Alt+Shift+F9

提交回复
热议问题