Apply conditional formatting to selected cells using VBA

Dear Experts:

I would like to apply conditional formatting to selected cells applying the following conditional formatting:

Interior.Color = RGB (243, 17,19)
Font.Color = RGB (255, 255, 255)

The conditional formatting is based on a formula:

=COUNTIF(A$2:A2,A2)=1 'example

The selection is always a vertical selection of cells and in the above example the selected cells start at A2. But of course the selection could also start a B5 and extend to B400 or C2 and extend to C1200

Is there a way to run a macro to apply the above conditional formatting to any vertical selection I wish to make.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
can you provide a sample with expected output here? it would be great for illustration.
0
Andreas HermleTeam leaderAuthor Commented:
Hi Ryan,

here we are, thank you very much.

Regards, Andreas

conditional_formatting_VBA.xlsx
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
sorry as don't have time at this moment to propose a proper solution.

but you can try refer to this and see if it helps

Override Conditional Formatting with Worksheet_SelectionChange
https://stackoverflow.com/questions/29869795/override-conditional-formatting-with-worksheet-selectionchange

which need to be customized.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Andreas HermleTeam leaderAuthor Commented:
ok, I will give this a try, thank you very much for it.
0
Rgonzo1971Commented:
Hi,

pls try
Sub Macro1()
    Set selRng = Selection.Resize(, 1) 'restrict to 1st column
    rng1 = selRng.Resize(1, 1).Address(1, 0)
    rng2 = selRng.Resize(1, 1).Address(0, 0)
    selRng.FormatConditions.Delete
    ' maybe change ","  to ";" and SUMMEWENN for your localization
    selRng.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=COUNTIF(" & rng1 & ":" & rng2 & "," & rng2 & ")=1"
    With selRng.FormatConditions(1)
        .Font.Color = RGB(255, 255, 255)
        .Interior.Color = RGB(243, 17, 19)
    End With
End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Andreas HermleTeam leaderAuthor Commented:
Wow, Rafael, I am really deeply impressed by your expertise. Works like a charm. I had to tweak it as you suggested, i.e. replace the english formula terms with the german formula arguments.

Really, a very nice job from your side, as always.

I really highly appreciate your expertise. Thank you very much.

Best Regards, Andreas
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
Rgonzo1971's solution will work but it will also remove the existing Conditional Formatting for selected cell, if that's fine for you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.