ConditionalFormat() for a range

jvanbonn

Board Regular
Joined
Mar 11, 2011
Messages
71
My goal is to have code to set a dynamic range to the variable myRng and then set conditional formatting such that if A(i) =true then row(i) is Grey, Italic, and Strikethrough.

What I notice happening is that the conditional format formula becomes "$A1048535=TRUE"

It acts like there is a application variable that keeps counting up every time I run the script.

Code:
[COLOR=Blue]Sub[/COLOR] CreateConditionalFormat(senior [COLOR=Blue]As String[/COLOR])
[COLOR=SeaGreen]'
' CreateStrikeGreyCondition Macro
'[/COLOR]
[COLOR=Blue]Dim[/COLOR] myRng [COLOR=Blue]As[/COLOR] Range

[COLOR=Blue]Set[/COLOR] myRng = Sheets(senior).[B4].CurrentRegion

Cells.FormatConditions.Delete
  [COLOR=Blue]With[/COLOR] myRng.FormatConditions _
    .Add(xlExpression, xlExpression, "=$A5=True")
    [COLOR=Blue]With[/COLOR] .Font
      .Color = RGB(90, 90, 90)
      .Italic = [COLOR=Blue]True[/COLOR]
      .Strikethrough = [COLOR=Blue]True[/COLOR]
    [COLOR=Blue]End With
  End With
End Sub[/COLOR]
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
What range do you want to apply this conditional formatting to?

What's the address of the cell in the top left of the range?

Is it in row 5?
 
Upvote 0
The upper left cell is always B4, but row 4 is always header information.
The number of rows varies, hence my use of :

Code:
Set myRng = Sheets(senior).[B4].CurrentRegion
 
Upvote 0
Well that's going to include the header row and therefore put the formula off, perhaps just by one row but perhaps more.

Excel VBA can be quite picky when setting conditional formatting using formulas.

This will work but it'll also apply the conditional formatting to the header row.
Code:
Option Explicit
 
Sub CreateConditionalFormat(senior)
'
' CreateStrikeGreyCondition Macro
'
Dim myRng As Range
 
    Set myRng = Sheets(senior).Range("B4").CurrentRegion
 
    Cells.FormatConditions.Delete

    With myRng.FormatConditions _
         .Add(xlExpression, xlExpression, "=R[0]C1")

        With .Font
            .Color = RGB(90, 90, 90)
            .Italic = True
            .Strikethrough = True
        End With

    End With
 
End Sub
If you don't want to include the header row or any other cells/ranges you'll need to use some other method to get the correct range.

Oh, forgot to mention - I changed the formula to R1C1 notation, VBA seems to 'like' that better for conditional formatting.:)
 
Upvote 0
Thanks Norie!

That fixed my problem.

Also, setting a conditional format for the header row is not a problem because A4 (R4C1) will never be TRUE.
 
Upvote 0
No problem.:)

Though the header row wouldn't really matter, you might want to be careful when using CurrentRegion for other things though.
 
Upvote 0
Do you have any suggestions to define a range of cells that always starts in R4C2 with variable number of additional rows and columns.

I've considered using the ".Cells(row.count,i).End(xlUp)" method, but column(i) of the last row may be blank.

Thanks again!
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,723
Members
452,939
Latest member
WCrawford

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