Excel User Defined Function: change the cell's color

前端 未结 6 1034
生来不讨喜
生来不讨喜 2020-12-11 11:10

I have a user defined function in Excel. It is called as a formula function from spreadsheet cells and works fine.

I\'d like the function to be able to change the ce

6条回答
  •  天涯浪人
    2020-12-11 11:43

    Function HexToLongRGB(sHexVal As String) As Long
        Dim lRed As Long
        Dim lGreen As Long
        Dim lBlue As Long
        lRed = CLng("&H" & Left$(sHexVal, 2))
        lGreen = CLng("&H" & Mid$(sHexVal, 3, 2))
        lBlue = CLng("&H" & Right$(sHexVal, 2))
        HexToLongRGB = RGB(lRed, lGreen, lBlue)
    End Function
    
    Function setBgColor(ByVal stringHex As String)
        Evaluate "setColor(" & Application.Caller.Offset(0, 0).Address(False, False) & ",""" & stringHex & """)"
        setBgColor = ""
    End Function
    

    Sub setColor(vCell As Range, vHex As String)
        vCell.Interior.Color = HexToLongRGB(vHex)
    End Sub
    

提交回复
热议问题