Conditional Formatting Macro

FredMcStaire

New Member
Joined
Jan 19, 2009
Messages
33
Hopefully a simple one:
I have a particular conditional formatting style I use for my error checking formulas. When the formula in the cell = 0, then one style appears and when the value <>0, then another format appears.

I'm struggling to build a piece of VBA ( or a macro) that I could run on the active cell to apply this conditional formatting, in particular how to make it cell-agnostic. For example: i would like to click on a cell, then click on a button in the QAT and have the conditional formatting applied, then select another cell and press on the same QAT button to have the same conditional formatting applied to the new cell.

Thanks!
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,391
Office Version
  1. 2016
Platform
  1. Windows
Code:
Sub v()
If Selection.Count <> 1 Then Exit Sub
If Selection = 0 Then
    Selection.Interior.ColorIndex = 4
Else
    Selection.Interior.ColorIndex = 7
End If
End Sub
 

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,696
If I understand correctly, you mean "creating conditional format for a cell" by using VBA. If that's the case, then following should help. It should be self descriptive, but let us know if any questions:

Code:
Sub setConditionalFormat()
Dim cll As Range
Dim fc As FormatCondition

    'Active cell
    Set cll = ActiveCell

    'First condition
    Set fc = cll.FormatConditions.Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:=0)
    fc.Interior.Color = 10284031
    
    'Second condition
    Set fc = cll.FormatConditions.Add(Type:=xlCellValue, Operator:=xlNotEqual, Formula1:=0)
    fc.Interior.Color = 14348258
    
End Sub
 

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,696
thanks Smozgur, exactly what I was after. I knew there had to be an elegant solution.

Glad to hear it helps.

In fact, it could have been two 1-liners instead bunch of assignments and codes, but I wanted to make readable and explain the FormatCondition object. In case you'd like to see the shorter version:

Code:
Sub setConditionalFormat()
    With ActiveCell.FormatConditions
        .Add(1, 3, 0).Interior.Color = 10284031
        .Add(1, 4, 0).Interior.Color = 14348258
    End With
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,117
Messages
5,835,489
Members
430,358
Latest member
zzc1128

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
Top