VBA RGB | Set Color Of Interior Cell Using RGB Function

MenuWallstreetMojoAll Courses search🛒LoginWallstreet LogoClose iconSign InWallstreet LogoClose iconAll Courses

Trending Courses

Course Categories

Certification Programs

Free Courses

View All CoursesChevron

VBA Resources

  • E-Books
  • Career Guides
  • Interview Prep Guides
  • Free Practice Tests
  • Excel Cheatsheets
VBA ResourcesVBA RGBBook IconFormatting in VBA
  • Toggle IconCell Formatting
    • Right arrowVBA Format
    • Right arrowVBA Format Number
    • Right arrowVBA Conditional Formatting
    • Right arrowVBA Borders
  • Toggle IconFont and Color
    • Right arrowVBA Font Color
    • Right arrowVBA Color Index
    • Right arrowVBA RGB
Formatting in VBAArrow down filled🏅 WSM MEMBERSHIPLogo

ALL COURSES@ADDITIONAL50% OFF

UNLOCK DEAL B_VectorGirl_ImageVBA RGB

Last Updated :

-

-

Blog Author :

Jeevan A Y

Jeevan A Y

Edited by :

Ashish Kumar Srivastav

Ashish Kumar Srivastav

Ashish Kumar Srivastav

Reviewed by :

Dheeraj Vaidya, CFA, FRM

Dheeraj Vaidya, CFA, FRM

Dheeraj Vaidya, CFA, FRM

Share icon

Share

Table Of Contents

arrow

Excel VBA RGB Color

RGB can also be called red, green, and blue. So, one may use this function to get the numerical value of the color value. This function has three components as a named range, and they are red, blue, and green. The other colors are the components of these three different colors in VBA.

In VBA, everything boils down to the coding of every piece. For example, we can use the RANGE object if you want to reference some portion of the worksheet. If you want to change the font color, we can use the NAME property of the range. Then, write the font name that we need but imagine a situation of changing the font color or the cell's background color. Of course, we can use built-in VB colors like vbGreen, vbBlue, vbRed, etc. But, we have a dedicated function to play around with different colors, i.e., RGB.

Table of contents
  • Excel VBA RGB Color
    • Change Color of Cells using VBA RGB Function
      • Example #1
      • Example #2
    • Things to Remember Here
    • Recommended Articles
VBA-RGB

Below is the syntax of the RGB color function.

VBA RGB Syntax

As you can see above, we can supply three arguments: red, green, and blue. These three parameters can only accept integer numbers ranging from 0 to 255. The result of this function will be the "Long" data type.

Change Color of Cells using VBA RGB Function

Example #1

We have numbers from cells A1 to A8, as shown in the below image.

VBA RGB Example 1

We will try to change the font color to some random color for this range of cells by using the RGB function.

Start the macro procedure first.

Code:

Sub RGB_Example1() End Sub
VBA RGB Example 1.0

First, we need to reference the range of cells of fonts we want to change the color of. In this case, our range of cells is A1 to A8, so supply the same using the RANGE object.

Code:

Sub RGB_Example1() Range ("A1:A8") End Sub
Example 1.2

Put a dot to see the IntelliSense list of RANGE objects. From the IntelliSense list, we are trying to change the font color, so choose the FONT property from the list.

Code:

Sub RGB_Example1() Range("A1:A8").Font End Sub
VBA RGB Example 1.3

Once we chose the FONT property in this property, we tried to change the color, so we chose the color property of the FONT.

Code:

Sub RGB_Example1() Range("A1:A8").Font.Color End Sub
Example 1.4

Put an equal sign and open the RGB function.

Code:

Sub RGB_Example1() Range("A1:A8").Font.Color = RGB( End Sub
VBA RGB Example 1.5

Give random integer numbers ranging from 0 to 255 for all three arguments of the RGB function.

Code:

Sub RGB_Example1() Range("A1:A8").Font.Color = RGB(300, 300, 300) End Sub
 Example 1.9

Now, run the code and see the result of font colors of the cells from A1 to A8.

Output:

VBA RGB Example 1.7.0

So, the colors of the font changed from black to some other. The color depends on the numbers we give to the RGB function.

Below are RGB color codes to get some of the common colors.

Example 1.8

You can just change the integer number combination from 0 to 255 to get the different sorts of colors.

Example #2

For the same range of cells, let us see how to change the background color of these cells.

First, supply the range of cells by using the RANGE object.

Code:

Sub RGB_Example2() Range ("A1:A8"). End Sub
VBA RGB Example 2

This time we are changing the background color of the mentioned cells, so we have nothing to do with the FONT property. Now, choose the "Interior" property of the RANGE object to change the background color.

Code:

Sub RGB_Example2() Range("A1:A8").Interior End Sub
Example 2.1

Once the “Interior” property is selected, a dot to see the properties and methods of this “Interior” property.

Code:

Sub RGB_Example2() Range("A1:A8").Interior. End Sub
VBA RGB Example 2.2

Since we are changing the interior color of the mentioned cells, choose the “Color” property.

Code:

Sub RGB_Example2() Range("A1:A8").Interior.Color End Sub
Example 2.3

Set the interior color property of the range of cells (A1 to A8) out the equal sign and open the RGB function.

Code:

Sub RGB_Example2() Range("A1:A8").Interior.Color = RGB( End Sub
VBA RGB Example 2.4

Enter the random number as you want.

Code:

Sub RGB_Example2() Range("A1:A8").Interior.Color = RGB(0, 255, 255) End Sub
Example 2.6

Run the code and see the background color.

Output:

VBA RGB Example 2.5.0

The background color has changed.

Things to Remember Here

  • RGB stands for Red, Green, and Blue.
  • A combination of these three colors will give different colors.
  • All these three parameters can accept integer values between 0 to 255 only. It will reset any numbers above this to 255.

Recommended Articles

This article has been a guide to VBA RGB. Here, we discuss changing the color of the interior cell (background, font) in Excel VBA by putting different integer numbers in the RGB function with examples and a downloadable Excel template. Below are some useful Excel articles related to VBA: -

  • VBA Font Color
  • Excel VBA Web Scraping
  • Color Index in VBA
  • Class in VBA
  • VBA MsgBox (Yes/No)
INVESTMENT BANKING RESOURCESLearn the foundation of Investment banking, financial modeling, valuations and more.Join Wallstreetmojo YoutubeYoutubeFREE EXCEL COURSESLearn MS Excel right from scratch. Master excel formulas, graphs, shortcuts with 3+hrs of Video.Join Wallstreetmojo InstagramInstagramFREE FINANCE MODELING IN EXCELLearn Financial Modeling in Excel with this Step by Step Guide (Colgate Case Study)Join Wallstreetmojo LinkedinLinkedInINVESTMENT BANKING RESOURCESLearn the foundation of Investment banking, financial modeling, valuations and more.Join Wallstreetmojo YoutubeYoutubeJoin Wallstreetmojo InstagramInstagram

Từ khóa » Visual Basic Color Rgb