Conditional Formatting Macro

FredMcStaire

New Member
Joined
Jan 19, 2009
Messages
36
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

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,947
Messages
6,175,559
Members
452,652
Latest member
eduedu

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