Please help me on Macro FormatConditions

albertxu

New Member
Joined
Jan 30, 2015
Messages
3
I tried to write a macro to format my excel file.

There are numbers in column G and the numbers range between 1 to 99 (all are integers). My target is to shade the row with color green when the number is equal to or less than 30, grassgreen when it's between 31 and 40, yellow when it's larger than 41 but less than 45. my code is as follows but it did not work, can anyone give me help?

Thanks a lot.

Code:
    ActiveSheet.UsedRange.Select
    
    Selection.FormatConditions.Delete
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$G2<45.1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
        Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$G2<40.1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5296274
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$G2<30.1"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 5287936
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Might be to do with the range it's using

Try changing
ActiveSheet.UsedRange.Select

to something like

ActiveSheet.Range("A2:M50").Select
 
Upvote 0
But I have hundreds of excel file need to be done with this macro, those files have different rows.

Might be to do with the range it's using

Try changing
ActiveSheet.UsedRange.Select

to something like

ActiveSheet.Range("A2:M50").Select
 
Upvote 0
this seems to work
Code:
Sub condFormat()
    ActiveSheet.UsedRange.Select
    
    Selection.FormatConditions.Delete

    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:=41, Formula2:=45
    Selection.FormatConditions(1).Interior.Color = RGB(255, 255, 0) 'yellow
    
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="31", Formula2:="40"
    Selection.FormatConditions(2).Interior.Color = RGB(0, 153, 51) 'grass green
    
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, Formula1:="31"
    Selection.FormatConditions(3).Interior.Color = RGB(0, 255, 0) 'green
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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