Using VBA to Conditionally Format- IF statement

usmananyc

New Member
Joined
Jul 21, 2015
Messages
31
Hello all, I have a spreadsheet (A3:AE25) I would like to conditionally format based on a specific rule type. First Two row are headers that arent to be touched.

Column C houses rules anywhere from 1-20. As an example, Rule 1 requires fields D3 and F3 to have a value so I would want this highlighted in green if there is a value present. If there is a blank value or text containing the word “Required”, this field should be formatted red.

Rule 1 might also have fields that are optional like E3 and G3. I would like these to be highlighted in Orange if there is a value present or if it has the text “optional” in it. It should be highlighted in red if there is a blank.

Other fields that are not supposed to have any values values should remain blank without any formats, but if it does have a value, I would like this to have a background color of red.

So basically it would be an IF statement (looking at column C, if rule type = 1, then....) There are multiple rules that have varying fields of required/optional so a VBA script that would conditionally format these specified cells would really make things easier and reduce human error. Any help in creating a template script for two rules would be great, the rest I would be able to add myself. Thanks in Advance!!
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I am not sure what you mean by represented exactly but it would just be a value ranging from 1-20. So if column C3 has a number 1 in it then it would imply this is rule type 1 so i would want to follow the logic for this rule type. I apologize if this sounds a bit confusing
 
Upvote 0
So where/how would these 'rules' be stored?
 
Upvote 0
I was thinking they would be stored in the VBA Logic/Script itself. They would have to be defined in an IF/OR statement.

If C3 = 1 and D3 = "Required'' or Blank then hightlight red or D3 = value then highlight in green OR
IF C4 = 2 and.... so on and so forth.

The code will get quite lengthy since each rule has a minumum of four cells to highlight but most of them are repeats with some minor differences in the cell that requires formatting.
 
Upvote 0
So the 'rules' would be hard-coded?
 
Upvote 0
Yes i suppose so. I dont see any other way for it to be done. That way when data gets pasted into the template sheet, the macro will know if the fields are populated correctly based on that specific rule.
 
Upvote 0
Here what i have so far, however it does not seem to run when i insert the IF statement. Any advice?

Code:
Sub ConFor()
If Sheet1.Range("C3") = "1" Then




    Range("D3").Select
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=LEN(TRIM(D3))>0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
    
    Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
        "=LEN(TRIM(D3))=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False


   
    Selection.FormatConditions.Add Type:=xlTextString, String:="Required", _
        TextOperator:=xlContains
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    
End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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