Conditional Formatting via Macro

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, I've trying to build a macro that, once we click, will automatically put the conditional formatting in the page. I have added the excel sheet to the google drive for your reference. It would be great if anyone can help me or guide me create a macro for this - New Microsoft Excel Worksheet.xlsx
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I cannot open insecure internet files, but here is an example.
VBA Code:
'
' This example will set a format condition to change the cell color
' for any cell greater than 20 and less than 50 for range A2 to A100
'

Sub SetFormatConditionsExample()
    Dim FCRange As Range
    Dim FormulaStr As String
    Dim WS As Worksheet
    
    Set WS = ActiveSheet                    'define worksheet containing format conditions.
    
    'a) Define the cell range
    Set FCRange = WS.Range("A2:A100")
    
    With FCRange.FormatConditions
        
        'b) Delete the existing format conditions
        .Delete
        
        'c) Define the formula
        FormulaStr = "=AND($A2>20,$A2<50)"         'Any formula that resolves to True or False"
        
        
        'd) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbGreen
            .Font.Color = vbBlack
        End With
    End With
End Sub
 
Upvote 0
I cannot open insecure internet files, but here is an example.
VBA Code:
'
' This example will set a format condition to change the cell color
' for any cell greater than 20 and less than 50 for range A2 to A100
'

Sub SetFormatConditionsExample()
    Dim FCRange As Range
    Dim FormulaStr As String
    Dim WS As Worksheet
   
    Set WS = ActiveSheet                    'define worksheet containing format conditions.
   
    'a) Define the cell range
    Set FCRange = WS.Range("A2:A100")
   
    With FCRange.FormatConditions
       
        'b) Delete the existing format conditions
        .Delete
       
        'c) Define the formula
        FormulaStr = "=AND($A2>20,$A2<50)"         'Any formula that resolves to True or False"
       
       
        'd) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbGreen
            .Font.Color = vbBlack
        End With
    End With
End Sub
Hi Thank you for your reply, I have 5 conditions to this, The file I have uploaded is on google drive but I'm uploading an image as well for your reference
 

Attachments

  • Capture.PNG
    Capture.PNG
    78.2 KB · Views: 29
Upvote 0
Like I said, I cannot open random files off the internet.
Hi Thank you for your reply, I have 5 conditions to this, The file I have uploaded is on google drive but I'm uploading an image as well for your reference

You can extend my example to add more rules:

VBA Code:
'
' This example will set a format condition to change the cell color
' for any cell greater than 20 and less than 50 for range A2 to A100 and range B2 to B100
'

Sub SetFormatConditionsExample()
    Dim FCRange As Range
    Dim FormulaStr As String
    Dim WS As Worksheet
    
    Set WS = ActiveSheet                    'define worksheet containing format conditions.
    

'Column A rules
    'a) Define the cell range
    Set FCRange = WS.Range("A2:A100")
    
    With FCRange.FormatConditions
        
        'b) Delete the existing format conditions
        .Delete
        
        'c) Define the formula
        FormulaStr = "=AND($A2>20,$A2<50)"         'Any formula that resolves to True or False"
        
        
        'd) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbGreen
            .Font.Color = vbBlack
        End With
    End With

'Column B rules
    'a) Define the cell range
    Set FCRange = WS.Range("B2:B100")
    
    With FCRange.FormatConditions
        
        'b) Delete the existing format conditions
        .Delete
        
        'c) Define the formula
        FormulaStr = "=AND($B2>20,$B2<50)"         'Any formula that resolves to True or False"
        
        
        'd) Add the new FormatCondition and define the formatting
        With .Add(Type:=xlExpression, Formula1:=FormulaStr)
            .StopIfTrue = True
            .Interior.Color = vbYellow
            .Font.Color = vbBlack
        End With
    End With

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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