How To Create A Microsoft Excel User Defined Function To Fill Cells With Color Using RGBCOLOR()

Vishal Verma
2 min readMar 29, 2021

--

Excel User Defined Function: Excel User Defined Function, also know as UDFs allows us to create custom functions in Microsoft Excel like built-in functions.

RGB (Red, Green, Blue) function is used to get the numerical value of the color, this function has three components as a named range and they are red, blue, and green. Other colors are considered as the components of these three different colors in VBA.

Let’s see an example of how to use Excel User Defined Function RGBCOLOR()

VBA Source Code:

Function RGBCOLOR(ByVal R As Integer, ByVal G As Integer, ByVal B As Integer) 
Dim CLR As Long, SRC As Range, S As String, C As String, E
If R > 255 Or G > 255 Or B > 255 Then
RGBCOLOR = ""
Exit Function
Else
CLR = RGB(R, G, B)
Set SRC = Application.ThisCell
S = SRC.Parent.Name
C = SRC.Address(False, False)
E = "Input_RGB(""" & S & """,""" & C & """," & CLR & ")"
SRC.Parent.Evaluate E
RGBCOLOR = ""
End If
End Function
Sub Input_RGB(S, C, CLR As Long)
ThisWorkbook.Sheets(S).Range(C).Interior.color = CLR
End Sub

Thank you for reading this article.

Please follow for more informative and engaging content.

--

--

Vishal Verma
Vishal Verma

Written by Vishal Verma

Blogger | Consultant | Senior Business Analyst | Data Science Enthusiast

No responses yet