Selection.FormatConditions

blbat

Active Member
Joined
Mar 24, 2010
Messages
338
Office Version
  1. 2016
  2. 2013
Is there a cleaner, faster way to for my code to take this:

Excel Workbook
G
1Days Open
22
310
411
511
611
711
815
915
1015
1121
1222
1323
1423
1524
1629
1767
1887
1995
20105
working
Excel 2007


and with click of a button get it conditionally formatted as follows:
(This code works, I was just looking for a better way)

Code:
Private Sub cmdDaysOpn_Click()
    Dim lLastRow As Long
 
    With ActiveSheet
         'find the last row in the worksheet
        lLastRow = Get_Last_Row(.Cells)
    End With
 
    'Select the Used Range in column G
    With ActiveSheet.Range("G2:G" & lLastRow).Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
 
    With Selection
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=89"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Font
            .Color = -16383844
            .TintAndShade = 0
        End With
 
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 3
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
 
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
        Formula1:="=60", Formula2:="=89"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 27
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=60"
        Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
        With Selection.FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 4
            .TintAndShade = 0
        End With
        Selection.FormatConditions(1).StopIfTrue = False
        End With
    End With
End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Only a minor changes:
1. You don't need to select the area you are working on.
2. You should always remove any previous conditions. With Excel 2007 and greater, you can have more than three conditions and without removing the old ones, you would get multiple overlapping conditions.
3. The last used row for the entire worksheet and the last used row for column G may differ, so I used a method to determine the last used cell in that range.

Code:
Private Sub cmdDaysOpn_Click()
    Dim lLastRow As Long
 
    lLastRow = Cells(Rows.Count, 7).End(xlUp).Row
 
    With ActiveSheet.Range("G2:G" & lLastRow)
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
 
        .FormatConditions.Delete 'Clear any existing conditional formats
 
        'Red
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
            Formula1:="=89"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Font
            .Color = -16383844
            .TintAndShade = 0
        End With
 
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 3
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
 
        'Yellow
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, _
            Formula1:="=60", Formula2:="=89"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 27
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
 
         'Green
        .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
            Formula1:="=60"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority
        With .FormatConditions(1).Interior
            .PatternColorIndex = xlAutomatic
            .ColorIndex = 4
            .TintAndShade = 0
        End With
        .FormatConditions(1).StopIfTrue = False
    End With
 
End Sub
 
Upvote 0
pbornemeier,

I appreciate the feedback.

I had to look close to see why I don't need to select the range-
Thanks for that!

Last Row- for this particular spreadsheet the last row will always be the same across the columns, but that is a excellent method to determine last used cell and I will remember it.

Clearing previous FormatConditions- I did not think about that and it might have caused some strange results. Thanks for that line as well.

regards,

blbat
 
Upvote 0

Forum statistics

Threads
1,224,614
Messages
6,179,906
Members
452,949
Latest member
beartooth91

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