VBA to get Excel Cell Background Color RGB Code & Change it
Hope you would like these in-depth Excel cell color formatting techniques.
In Excel macro to get cell color in RGB code or change it, we can follow any of these methods.
- Excel VBA get RGB Color of Cell or Convert Cell Color to RGB Code
- VBA: Excel Color index to change cell color
- Manual: Excel Background Color of Cell
This page is all about Excel Cell formatting.
Let’s see with an example on how to play with this RGB code.
1. Excel VBA Get RGB Color of Cell
To do this, first get the color code of a cell using its Cells.Interior property.
Excel will return a hexadecimal value. Which can be to its corresponding RGB value using the formula in the vba code.
Once we are familiar with this code, then it will be easy to
Excel VBA get RGB color of cellVisual Basic123456789101112Sub Excel_VBA_Get_RGB_Color_Of_Cell()Dim cColor, cRed, cGreen, cBlue'Get Cell background color VBA Value. Convert this Color Value to RGB ValuecColor = ActiveSheet.Cells(1, 1).Interior.Color'Convert Color Code to RGBcRed = (cColor Mod 256)cGreen = (cColor \ 256) Mod 256cBlue = (cColor \ 65536) Mod 256MsgBox cColor & "; " & VBA.RGB(cRed, cGreen, cBlue)End Sub
- Get the Excel color index of a cell.
- Change cell Interior Color in VBA.
- Change cell color based on value in another cell.
- Count number of cells based on its color.
Many times, we have to convert this hexadecimal to RGB code to use it in with many build in function calls. So, this Excel VBA get RGB color of cell will be much useful in many ways.
Above VBA code converts the cell color to its corresponding RGB code. The below code does the reverse.
2. Excel VBA Change Cell Color Index using RGB code
Using this Excel VBA, change cell background color.
Also if you need to get Cell background color property in a VBA variable field to use it inside a IF loop. The code below serves both the purpose.Excel VBA Change Cell ColorVisual Basic123456789101112131415161718192021222324Sub Excel_VBA_Change_Cell_Color_RGB()'Set Red, Green, Blue Color Coding levelsR = 50G = 150B = 255'Get the Corresponding RGB CodeDim RGBCode As LongRGBCode = VBA.RGB(R, G, B)'Excel VBA Change Cell Color Macro CodeSheets(1).Cells(1, 1).Interior.Color = RGBCode'If a Cell color has to be changed based on another cell value,'then embed the above code inside a IF condition'Cell Interior Color VBA'Cell background Color in VBASheets(1).Cells(1, 1) = Sheets(1).Cells(1, 1).Interior.Color'Additional Formating info'Similar to Background color, fond color also can be changed with below code'Sheets(1).Cells(1, 1).Font.Color = RGBCodeEnd Sub
This code can also be used to change the background color of a cell based on a cell value. It can be used as an alternative for conditional formatting.
3. Change Excel Cell Color Background or Font Color
To do this manually, we have to first select cell & change Excel color index as below
- Select ‘Home’ Menu.
- Click ‘Fill Color’ in ‘Font’ tab.
- Choose a color from the drop down or
- Right click on cell & choose Format Cells option to perform all the formatting changes
This will change the cell background color or range selected by user. But there are times, where we have to change the background color or font color based on condition within a VBA code. This can be done by using the Interior.Color property of Excel cell discussed above.
With this property the Excel Color Index, Format and Theme of an worksheet cell can be modified, during the VBA Macro runtime.
4. Open VBA Color Palette
This is another method to get user input using the built in color palette in Excel.Choose Color PaletteVisual Basic12345678910Sub Open_Color_Palette_Set_Color()Dim ret As Boolean'Select a Cell before invoking this command.ThisWorkbook.Sheets(1).Cells(1, 1).Select'Application.Dialogs.Item(xlDialogColorPalette).Show'Application.CommandBars("Fill Color").Visible = TrueApplication.Dialogs(xlDialogPatterns).ShowEnd Sub
This command has to select a cell within the code before opening color palette. Once executed, this code will open the color palette allowing user to choose a color. Once the color is chosen, the selected cell background will be applied with that color.
Excel VBA Cell Background Color & Formatting
We saw mainly on how to get RGB code for a cell color & change the cell background color using VBA . Once if we know how to get or set a color code through VBA, it will be easy to manipulate them inside our coding.
In addition to font and background color, we can also do the below list of cell formatting operations.
- Wrap Text.
- Sheets(1).Cells(1, 1).WrapText = True
- Sheets(1).Cells(1, 1).HorizontalAlignment = xlCenter
- Font Change
- Sheets(1).Cells(1, 1).Font.Name = “Arial”
- Font Size
- Sheets(1).Cells(1, 1).Font.Size = 12
- Font Bold or Italic
- Sheets(1).Cells(1, 1).Font.Italic = True
There are plenty of such option available to change the format of a cell. We can try to record macro while manually doing formatting changes and get the corresponding code to perform these Formatting.
Additional Reference on Cell Color & Interior Property
Excel VBA – Get RGB Color of Cell – Change Background
- Games VCF Countif Record Macro Timer in Excel Task Manager print screen HTML Table Import Data Thumbnails Text to Speech TTS Candy Crush Block Apps in FaceBook C++ Programing Machine learning AI bulk email Excel VBA vba color index vba color codes Android social media Twitter Google+ Google Adsense Tips bitcoin Calendar in Excel Excel Macro outlook vba mass email Data Mining Facebook WhatsApp python Wordpress