CELL FORMATTING

Keebler

Board Regular
Joined
Dec 1, 2021
Messages
172
Office Version
  1. 2021
Platform
  1. Windows
is there a vba code that will change the formatting of a cell (cell background color, font color, etc) based on what color name the cell has in it, referencing a range?

for example
in cell G2:G20 I have a range of colors (RED, BLU, BLK, GRN, etc)
in another sheet (data) in range a5:e20 I have a vlookup table
col1 has the short name (3 letter - RED, BLU, GRN, etc)
col 2 has the full name (RED, BLU, GREEN, YELLOW, ETC)
col 3,4,5 have the RGB CODES (red, grn, blu)
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You haven't specified the sheet name of the G2:G20 cells, so this macro expects that sheet to be the active sheet.

VBA Code:
Public Sub Format_Cells()

    Dim lookupTable As Range
    Dim cell As Range
    Dim dataRow As Variant
    
    Set lookupTable = Worksheets("Data").Range("A5:E20")
    
    For Each cell In ActiveSheet.Range("G2:G5")
        dataRow = Application.Match(cell.Value, lookupTable.Columns(1), 0)
        If Not IsError(dataRow) Then
            cell.Interior.Color = RGB(lookupTable.Item(dataRow, 3).Value, lookupTable.Item(dataRow, 4).Value, lookupTable.Item(dataRow, 5).Value)
        End If
    Next
    
End Sub
 
Upvote 1
Solution
would it be fair to assume that adding on_change to the header would result in every time the worksheet changes (there is no name other than sheet 1 (or active sheet)) the vba code would run?
the active sheet range is g5:g20 ( easy enough to do here)
thank you, this looks right up the alley. ill try it out later tonight or tomorrow when i get back home --- thank you
 
Upvote 0
Private Sub Format_Cells()

Dim lookupTable As Range
Dim cell As Range
Dim dataRow As Variant

Set lookupTable = Worksheets("Data").Range("A5:E20")

For Each cell In ActiveSheet.Range("G5:G70")
dataRow = Application.Match(cell.Value, lookupTable.Columns(1), 0)
If Not IsError(dataRow) Then
cell.Interior.Color = RGB(lookupTable.Item(dataRow, 3).Value, lookupTable.Item(dataRow, 4).Value, lookupTable.Item(dataRow, 5).Value)
End If
Next

End Sub



what would i need to include in the vlookup table for the backgrounds that are too dark? i want to make the font on only these cells white. the others can remain black

thank you again

20240714 FCC maping.xlsm
GH
4
5WHT
6BLK
7GRN
8BLU
9YEL
10RED
11
12
13ORGWHITE
14YEL
15GRY
16GRN
17RED
18BLU
19VIO
20BRN
21ORGBLACK
22YEL
23GRY
24GRN
25RED
26BLU
27VIO
28BRN
29LTBLU
30LTGRN
31GRY
32
Sheet1
 
Upvote 0
so, i took what you did and added a few more columns to the vlookup table and added a font color row

Private Sub Format_Cells()

Dim lookupTable As Range
Dim cell As Range
Dim dataRow As Variant

Set lookupTable = Worksheets("Data").Range("A5:L20")

For Each cell In ActiveSheet.Range("G5:J70")
dataRow = Application.Match(cell.Value, lookupTable.Columns(1), 0)
If Not IsError(dataRow) Then
cell.Interior.Color = RGB(lookupTable.Item(dataRow, 3).Value, lookupTable.Item(dataRow, 4).Value, lookupTable.Item(dataRow, 5).Value)
cell.Font.Color = RGB(lookupTable.Item(dataRow, 10).Value, lookupTable.Item(dataRow, 11).Value, lookupTable.Item(dataRow, 12).Value)
End If
Next

End Sub


20240714 mapingv2.xlsm
G
5WHT
6BLK
7GRN
8BLU
9YEL
10RED
11
12
13ORG
14YEL
15GRY
16GRN
17RED
18BLU
19VIO
20BRN
21ORG
22YEL
23GRY
24GRN
25RED
26BLU
27VIO
28BRN
29LTBLU
30LTGRN
31GRY
Sheet1
 
Upvote 0
so, i took what you did and added a few more columns to the vlookup table and added a font color row

Here's another approach you could consider. Instead of 6 columns for the RGB fill colour and font colour for each short name, you could have 1 cell formatted with the required fill and font colours for each short name:

Loop rows lookup table set cell colour.xlsm
ABC
4Short NameFull NameFont
5REDREDXXXX
6BLUBLUEXXXX
7GRNGREENXXXX
8YELYELLOWXXXX
Data


The macro now uses the Format Painter to copy each 'Font' cell format (which could include any font setting such as underline and borders) and paste it to each destination cell.

VBA Code:
Public Sub Format_Cells2()

    Dim lookupTable As Range
    Dim cell As Range
    Dim dataRow As Variant
    
    Set lookupTable = Worksheets("Data").Range("A5:C8")
    
    For Each cell In Worksheets("Sheet1").Range("G2:G5")
        dataRow = Application.Match(cell.Value, lookupTable.Columns(1), 0)
        If Not IsError(dataRow) Then
            lookupTable.Item(dataRow, 3).Copy
            cell.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        End If
    Next
    Application.CutCopyMode = False
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top