VBA Conditional Formatting Problem (simple?)

randomwit

New Member
Joined
Apr 2, 2010
Messages
16
Background: I am trying to use a macro to apply conditional formatting to each column in a selection (FormatArea).

Each column needs to have two conditional formats--one for if the numbers are greater than a cell and one for if they are less than another cell. These two cells are unique to each column (they are in rows 2 and 4 of each column).

I think my problem is in the Format1 property of the conditional formats. This code runs, but doesn't actually format the cells as expected.

HELP?

Code:
Sub AddConditionalFormats(FormatArea As Range)
    
    Dim col As Range
    Dim ucl As Range
    Dim lcl As Range
    
    FormatArea.FormatConditions.Delete
       
    For Each col In FormatArea.Columns
        
        Set ucl = Cells(4, col.Column)
        Set lcl = Cells(2, col.Column)
        
        col.Select
        
        'GREATER THAN UCL
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:=CStr(ucl.Address)
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        
        With Selection.FormatConditions(1).Font
            .Color = -16383844
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        
        'LESS THAN LCL
        'Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="=$C$2"
        'Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        'With Selection.FormatConditions(1).Font
            '.Color = -16383844
            '.TintAndShade = 0
        'End With
        'Selection.FormatConditions(1).StopIfTrue = False
        
    Next col
              
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Posting this gave me an idea that solved it!

Instead of ucl.Address i used ucl.Value....always the simple things
 
Upvote 0
On another note:

I realized that using .value will not link the formatting to the cell as I originally intended. The cells I want to link to are dynamic and the formatting should adjust accordingly...

Anyone know the answer to this one?
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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