EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login

VBA RGB

By Ashwani JaiswalAshwani Jaiswal

Home » VBA » Blog » VBA Tips » VBA RGB

VBA RGB

Excel VBA RGB

VBA RGB, where RGB can be expanded by Red, Green, and Blue. And this helps in changing the color of font and cell color as well. In VBA, every color is given a value by bifurcating its shades in red, green, and blue colors. And such we have a total of 56 shades of basic colors whose RGB values range from 0 to 255.

When we apply VBA RGB, we can apply either change the color cell background or we can change the font color. Below we have the syntax of VBA RGB.

VBA RGB Syntax

As we can see the syntax of RGB which clearly states that for each Red, Green, and Blue, it only considers the value as Integer. And together we can count RGB values as Long data type as well. We will be seeing the implementation of the RGB in upcoming examples. Before we move ahead, let’ see one of the basic colors which we could form using RGB as shown below.

VBA RGB Basic Colors

Change Color of background Cell Using RGB

We will learn how to change the color of the background cell in Excel by using the VBA Code.

You can download this VBA RGB Excel Template here – VBA RGB Excel Template

Example #1

In this example, we will see the simplest way to use RGB in VBA. Before we move forward with our Excel VBA RGB Code, first let us have data that we will be using to change the color of their cell or fonts. Below we have listed on some number from cell A1 to A10, which we will be using this example. For this, follow the below steps:

VBA RGB Cells

Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.

Popular Course in this category
All in One Excel VBA Bundle (120+ Courses)120+ Online Courses | 500+ Hours | Verifiable Certificates | Lifetime Access
4.9 (2,356 ratings)
Course Price

View Course

Related Courses

Insert Module

Step 2: Now in the newly opened Module write the subprocedure of VBA RGB.

Code:

Sub VBA_RGB()

End Sub

VBA RGB Example 1-1

Step 3: First we will select the cells, whose color we want to change. Here our range of cell will be from A1 to A10.

Code:

Sub VBA_RGB()

Range("A1:A10").

End Sub

VBA RGB Example 1-2

Step 4: Now to change the color fonts of the selected cells, we will use the FONT function followed by COLOR.

Code:

Sub VBA_RGB()

Range("A1:A10").Font.Color

End Sub

VBA RGB Example 1-3

Step 5: Now we will be using RGB with some random values to change the color Fonts from cell A1 to A10. We are giving more weightage to Red color and lesser to Green and Blue respectively as shown below.

Code:

Sub VBA_RGB()

Range("A1:A10").Font.Color = RGB(200, 100, 50)

End Sub

VBA RGB Example 1-4

Step 6: Now compile the code by pressing function key F8 and to run the code, click on the Play button located below the menu bar. We would see the color fonts of selected range cells nearer to Brown color.

VBA RGB Example 1-5

Example #2

In this example, we will be using the same data set which we saw in example-1 which is located in cell A1 to A10. Earlier we have seen the code to change the font colors but here we will be seeing how to change the color of a cell or background color or interior color. For this, again we can use the same line of code and do some editing or we can use a new module. We will be using the fresh module here. For this, follow the below steps:

Step 1: Open a new module and write the subprocedure for VBA RGB.

Code:

Sub VBA_RGB2()

End Sub

VBA RGB Example 2-1

Step 2: Now select the same sets of cell range from A1 to A10.

Code:

Sub VBA_RGB2()

Range("A1:A10").

End Sub

Range Example 2-2

Step 3: To change the interior color, we will be using the INTERIOR function along with COLOR in the same format which we saw in Example-1.

Code:

Sub VBA_RGB2()

Range("A1:A10").Interior.Color

End Sub

Interior Color Example 2-3

Step 4: Now we will be using RGB to fill the new color to the selected cell. We will be now giving more weightage to Green color and lesser to Red and Blue colors respectively.

Code:

Sub VBA_RGB2()

Range("A1:A10").Interior.Color = RGB(0, 255, 50)

End Sub

VBA RGB Example 2-4

Step 5: Now compile the code by pressing function key F8 and to run the code, click on the Play button located below the menu bar. we will see the interior color of the selected cells is now changed to a major Green color.

Interior Color Example 2-5

Step 6: Let’s try to change and put the value of Green color more than 255 and see what color we get.

Code:

Sub VBA_RGB2()

Range("A1:A10").Interior.Color = RGB(0, 400, 50)

End Sub

VBA RGB Example 2-6

Step 7: Now again run this updated code. We will notice that interior color selected cells A1 to A10 remains unchanged like previous, which is because RGB value resets to 255 if we use value more than 255.

Interior Color Example 2-7

Pros of VBA RGB

  • It is so easy to change the font color and interior color using RGB with the help of code shown in the above examples.
  • We can create and fill any type of color in which we want to see a font or in the cell.
  • Using RGB we can automate the process of changing the color of cells or fonts which is quite useful to identify the difference of data.

Things to Remember

  • We can try any type of combination of Red, Green, and Blue colors in RGB to see different shades of color.
  • We can form any type of color using RGB in Excel VBA.
  • The value of any color parameter can only be between o and 255. If we try to use value more than 255, it would be reset to 255 again.
  • Always save the file in the Macro enable format to retain the written code to be used again multiple times.

Recommended Articles

This is a guide to the VBA RGB. Here we discuss how to change the color of background cell in Excel by using the VBA Code along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. How to Use VBA Login?
  2. VBA Month | Examples With Excel Template
  3. How to Use Create Object Function in VBA Excel?
  4. How to Use VBA IsError Function?

All in One Excel VBA Bundle (120+ Courses)

120+ Online Courses

500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
VBA Tips
  • VBA Tips
    • VBA RGB
    • VBA Web Scraping
    • VBA AutoFill
    • VBA GoTo
    • VBA Color Index
    • VBA Wait
    • VBA Paste
    • VBA Copy Paste
    • VBA Remove Duplicates
    • VBA Sleep
    • VBA Font Color
    • VBA PowerPoint
    • VBA Borders
    • VBA Pivot Table
    • VBA Save As
    • VBA Tutorial For Beginners
    • VBA Charts
    • VBA Dictionary
    • VBA Conditional Formatting
    • VBA Paste Values
    • VBA Pause
    • VBA Refresh Pivot Table
    • VBA Macros
    • VBA Examples
    • Programming in Excel
    • VBA SendKeys
    • VBA Save Workbook
    • VBA PasteSpecial
    • VBA Function in Excel
    • VBA Visual Basic Applications
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

Download VBA RGB Excel Template

Already registered !

Special Offer - All in One Excel VBA Bundle (120+ Courses) Learn More