VBA Format cells in selection differently based on cell content

OVH

New Member
Joined
Nov 27, 2012
Messages
25
Super users,

I am new to vba and I can't really decipher what I've found online. Basically what I want to do is select a range (or entire sheet) and run a macro that will format each cell individually based on cell contents.

So basically if a cell contains an "X" then format as blue font, if it contains a "Y" then format as green font, etc. for about 5 variables.

My current if statements just format all of the cells in the range exactly the same.

Thank you in advance!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,142
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
This is an example. You could use if statements or select case statements
Code:
Sub formatrange()

Dim rn As Range


For Each rn In Selection
    If rn.Value = "X" Then
        rn.Font.Color = -4165632 'blue colour
    ElseIf rn.Value = "Y" Then
        rn.Font.Color = -16711681 'yellow colour
    End If
Next rn
    
    
End Sub
 
Upvote 0

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
38,154
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Give this a try (change the "Value_1", "Value_2",... to the actual text in the cells that you want to test for and change the ColorIndex values as well)...
Code:
Sub ColorCells()
  Dim Cell As Range
  For Each Cell In Selection
    Select Case Cell.Value
      Case "Value_1"
        Cell.Interior.ColorIndex = 3
      Case "Value_2"
        Cell.Interior.ColorIndex = 4
      Case "Value_3"
        Cell.Interior.ColorIndex = 5
      Case "Value_4"
        Cell.Interior.ColorIndex = 6
      Case "Value_5"
        Cell.Interior.ColorIndex = 7
    End Select
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,195,582
Messages
6,010,581
Members
441,557
Latest member
Jbest23

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
Top