2

I have this code which shows rgb color of target cell:

Function getRGB(RefCell)
Dim mystr As String
Application.Volatile
    mystr = Right("000000" & Hex(RefCell.Interior.Color), 6)
    getRGB = Application.Hex2Dec(Right(mystr, 2)) & ", " & _
             Application.Hex2Dec(Mid(mystr, 3, 2)) & ", " & _
             Application.Hex2Dec(Left(mystr, 2))
End Function

I need that this code instead of showing off rgb of other cell, would change background color of its own cell. Maybe anyone know how to do it?

Community
  • 1
  • 1
ArnoldasM
  • 186
  • 1
  • 3
  • 16

3 Answers3

5

The MSDN KB says

A user-defined function called by a formula in a worksheet cell cannot change the environment of Microsoft Excel. This means that such a function cannot do any of the following: Insert, delete, or format cells on the spreadsheet.

That unfortunately is incorrect!!!

YOU CAN change the color of the cell from where the formula is called. Here is an example. This will change the color of the cell to Red from where the formula is called.

The trick is to pass a blank value to the sub as the first parameter (a in the below case.)

Why does it work?

I don't know! But it works :)

Function SetIt(RefCell)
    RefCell.Parent.Evaluate "getRGB(" & """""" & "," & RefCell.Address(False, False) & ")"

    SetIt = ""
End Function

Sub getRGB(a As String, RefCell As Range)
    RefCell.Interior.ColorIndex = 3 '<~~ Change color to red
End Sub

ScreenShot

enter image description here

EDIT (Credit Where Due): I had seen this thread by Tim Williams long time ago and I had experimented with it and I had achieved lot of other things which that KB article says is not possible.

BTW I played more with it and I was able to make it work without passing a blank string.

Function SetIt(RefCell)
    RefCell.Parent.Evaluate "getRGB(" & RefCell.Address(False, False) & ")" 
    SetIt = ""
End Function

Sub getRGB(RefCell As Range)
    RefCell.Interior.ColorIndex = 3
End Sub

EDIT

Followup from Duplicate question and chat (Below comments)

Paste this in a code module and then in cell P20 paste the formula =setit(P20,N20)

Function SetIt(DestCell As Range, RefCell As Range)
    RefCell.Parent.Evaluate "SetAndGetRGB(" & RefCell.Address(False, False) & _
                                        "," & _
                                        DestCell.Address(False, False) & ")"

    SetIt = ""
End Function

Sub SetAndGetRGB(RefCell As Range, DestCell As Range)
    Dim sRGB As String
    Dim shName As String

    shName = Split(RefCell.Value, "!")(0)
    sRange = Split(RefCell.Value, "!")(1)

    sRGB = Right("000000" & Hex(Sheets(shName).Range(sRange).Interior.Color), 6)

    DestCell.Interior.Color = RGB( _
                                    Application.Hex2Dec(Right(sRGB, 2)), _
                                    Application.Hex2Dec(Mid(sRGB, 3, 2)), _
                                    Application.Hex2Dec(Left(sRGB, 2)) _
                                  )
End Sub

enter image description here

Note: I have not done any error handling. I am sure you can take care of that.

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
0

Since you cannot set the color of a cell using a Function called as a UDF, you would need to use a sub instead.

Pretty simple example:

Function CopyColor(RefCell As Range, DestCell As Range)
    DestCell.Interior.Color  = RefCell.Interior.Color
End Function
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
0

Siddharth's solution looks good. If you wish to have such function across the sheet without entering formula, put this code on it's VBA page. It will check changes in cells every time the content changes and you can use it to change the color if the content corresponds to the color format:

Private Sub Worksheet_Change(ByVal Target As Range)

' Test if a cell contains the proper formatting
' If it does, assign color
Target.Interior.ColorIndex = Target.Value

End Sub