vba help for Conditional Formatting

rsadambrown

New Member
Joined
Feb 14, 2019
Messages
2
Hi everyone! I'm looking at throwing in some conditional formatting to a macro I am writing. The range is dynamic and can change, and the conditional formatting is based off of another cell. I've tried recording the macro and copy+pasting that result, but the result is just making the entire selection red, instead of just the cells that meet the condition. I've tried some searching on here and in google, to no avail.

Here's a code sample. When stepping threw the macro, the first two lines select the range correctly, but when selection.style is executed, it just makes the whole range 'bad'. The intent is just to make any value in the range > the value in cell C25 on the 'Macro' tab 'bad'.

Code:
Set TestArea = Application.Range(Cell1:="G2", Cell2:="G" & EndingDown)TestArea.Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=Macro!$C$25"
Selection.Style = "Bad"

Thanks, and I appreciate any help!
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
the last row in your code just sets everything to style BAD. With this code you apply Formatting, rather than Conditional formatting:
Code:
Set TestArea = Application.Range(Cell1:="G2", Cell2:="G" & EndingDown)
TestArea.Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=Macro!$C$25"
[COLOR=#0000ff]    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority[/COLOR]
[COLOR=#0000ff]    With Selection.FormatConditions(1).Interior[/COLOR]
[COLOR=#0000ff]        .PatternColorIndex = xlAutomatic[/COLOR]
[COLOR=#0000ff]        .Color = 255[/COLOR]
[COLOR=#0000ff]  [/COLOR][COLOR=#0000ff]   End With[/COLOR]
You can adjust the formatting in the WITH block as you like it. you may even use the macro recorder to get the values you need.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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