VBA RGB | Change Color Of Background Cell Using RGB In VBA Excel
Có thể bạn quan tâm
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.
Watch our Demo Courses and Videos
Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.
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.
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 TemplateExample #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:
Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.
Step 2: Now in the newly opened Module write the subprocedure of VBA RGB.
Code:
Sub VBA_RGB() End SubStep 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
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 SubStep 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 SubStep 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.
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 SubStep 2: Now select the same sets of cell range from A1 to A10.
Code:
Sub VBA_RGB2() Range("A1:A10"). End SubStep 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 SubStep 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 SubStep 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.
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 SubStep 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.
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 –
- How to Use VBA Login?
- VBA Month | Examples With Excel Template
- How to Use Create Object Function in VBA Excel?
- How to Use VBA IsError Function?
Từ khóa » Visual Basic Color Rgb
-
RGB Function (Visual Basic For Applications) - Microsoft Docs
-
RGB Function (Visual Basic For Applications) | Microsoft Docs
-
VBA RGB | Set Color Of Interior Cell Using RGB Function
-
RGB Colours - Excel At Finance
-
Excel VBA: ColorIndex Codes List & RGB Colors
-
RGB Function - VB & VBA In A Nutshell: The Language [Book]
-
Excel VBA Color Code List - ColorIndex, RGB Color, VB Color
-
Using The RGB Function - Event Procedures - Visual Basic Planet
-
VB6 RGB Color: Chart, Codes
-
How To Enter RGB Code For Set Color On Form In VB NET 2012
-
Create Color From R G B Values - VB.Net Tutorial
-
VBA Course: Colors - Excel
-
Excel VBA Color Code List - ColorIndex, RGB Color, VB Color - Pinterest