VBA .FormatConditions in With Statement

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

I am rewriting VBA in an excel macro workbook that was originally created with the VBA Recorder. I tried to clean it up, but was unsure if I could get rid of every reference to the "Selection" of a range of values.

Is my syntax OK? Do I need to change parts of it? What can I do to get this to work?

FYI - mainSh12 is a reference to a Worksheet Object.

Code:
Set Rng24 = mainSh12.Range("P2:P" & lastrowSEres)
        
        With Rng24
            .Select
            .FormatConditions.Delete 'Either this line, or the 4th is Unnecc.
            .FormatConditions.Add Type:=xlExpression, Formula1:="=P2=""Research"""
            .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
            .FormatConditions(1).Interior.Color = 65535
            .FormatConditions(1).StopIfTrue = False
        End With
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
        With rng24
            .FormatConditions.delete 'Either this line, or the 4th is Unnecc.
            .FormatConditions.Add Type:=xlExpression, Formula1:="=P2=""Research"""
            .FormatConditions(rng24.FormatConditions.count).SetFirstPriority
            .FormatConditions(1).Interior.Color = 65535
            .FormatConditions(1).StopIfTrue = False
        End With
 
Upvote 0
Suggest you check it gives the result you want - the correct conditional format on the range.

In my experience, with conditional formatting via VBA taking out the range.select step will result in the formulas referencing the wrong cells,
UNLESS you use R1C1 notation.

So when applying conditional format (in VBA) and there are formulas referring to range addresses use R1C1 notation (then you're OK without the .Select)

cheers
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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