VBA function that returns color RBG code of a referenced cell I have this function I'm building to return the RBG code of a cell I input.  The code "works" but something funny is going on.  If I write multiple functions each referencing different cells, then all the functions simultaneously show the last cell I run through the function.

I'm probably doing something trivial here, but I'm also new to VBA.

 Public Function ColorCode(Reference As Range) As Variant

' Function to return RGB code of a single cell when referenced

Dim RGBColor As Long

Dim Red As Long, Green As Long, Blue As Long

Range(Reference.Address).Activate
RGBColor = Selection.Interior.Color

    Red = &amp;HFF&amp; And RGBColor

    Green = (&amp;HFF00&amp; And RGBColor) \ 256

    Blue = (&amp;HFF0000 And RGBColor) \ 65536

ColorCode = Red &amp; "/" &amp; Green &amp; "/" &amp; Blue

End Function
I don't think you need to activate the cell, or use the selection, just inspect the range directly.

I've also made the function volatile so it will update when you calculate.

But, for Code Review purposes, you're only getting the interior color, but the cell might not actually appear in that color, for example:


If there is conditional formatting on the cell, the interior color will not be accurate.
If there is a style on the cell, and the style applies a fill, then the interior color will not be accurate.
If the cell's interior has a 2-color fill, then then the interior color doesn't necessarily reflect either color of the 2-color fill.


Also, you're using some nice concise bit-shifting, and you'll have noticed that VBA doesn't have any shifting operators, but you might consider using some bit-shifting functions to make the code slightly clearer (and you can re-use them everywhere else that you need to shift bits.

And finally, unless you're certain that reference will always be a single celled reference, the color will default to 0/0/0 if there are more than 2 cells in the range and they have different colors.

 Public Function ColorCode(Reference As Range) As Variant
   Application.Volatile
' Function to return RGB code of a single cell when referenced

Dim RGBColor As Long

Dim Red As Long, Green As Long, Blue As Long

RGBColor = Reference.Interior.Color

    Red = &amp;HFF&amp; And RGBColor

    Green = (&amp;HFF00&amp; And RGBColor) \ 256

    Blue = (&amp;HFF0000 And RGBColor) \ 65536

ColorCode = Red &amp; "/" &amp; Green &amp; "/" &amp; Blue

End Function