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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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