Macro to affect cell background color

jmk15315

Board Regular
Joined
Nov 7, 2021
Messages
73
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
Good morning.

I have a spreadsheet that has a column for a date after an entry of a price. I want to create a macro that will color the cell red (or a shade of red) if the value of a specific cell is true. I have the following code that works, but I would like to know if there is a way to apply this to a range of cells, without having to create an IF/Else statement for each cell I want to affect. This is what I have currently.


VBA Code:
    If Range("S3").Value = True Then
        Range("U3").Select
        Selection.Font.Bold = True
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
    Else
        Range("U3").Select
        Selection.Font.Bold = False
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
    End If

End Sub

The cells I want to change, based on the based on the TRUE/FALSE value in the corresponding rows Column "S" are ("U3:U8,U10:U12,U14:U41")

I would guess there is a way to accomplish this but I am at a loss. Any assistance would be welcomed
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try:
VBA Code:
Sub ColorCells()
    Application.ScreenUpdating = False
    Dim rng As Range
    For Each rng In Range("U3:U8,U10:U12,U14:U41")
        If rng.Offset(, -2).Value = True Then
            rng.Font.Bold = True
            With rng.Interior
                .Pattern = xlSolid
                .PatternColorIndex = xlAutomatic
                .ThemeColor = xlThemeColorAccent2
                .TintAndShade = 0.399975585192419
                .PatternTintAndShade = 0
            End With
        Else
            rng.Font.Bold = False
            With rng.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent3
            .TintAndShade = 0.399975585192419
            .PatternTintAndShade = 0
        End With
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Upvote 1
Added an End If before "Next rng", but works perfectly. Thanks for your assistance.
 
Upvote 0
Oops!!! Thanks for catching that and you are very welcome. :)
 
Upvote 0

Forum statistics

Threads
1,223,931
Messages
6,175,465
Members
452,646
Latest member
tudou

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