Conditional formatting by code based on different conditions

olympiac

Board Regular
Joined
Sep 26, 2010
Messages
158
I need to format a report on a daily basis:
I need to check the values in three different columns and if it matches the rule below , I need to highligh the row in yellow.

"Rule 1":
If the code in "column A" is "ABCD" or "EFGH" or "IJKL" and the number in "column B" is over "7" and the amount in "column C" is over"1000" or over "-1000" then the row must be highlighted in Yellow
"Rule 2" and "Rule 3" are similar than "Rule 1" with different code and amounts.

How to achieve this by code?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try

Code:
Sub fmt()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        If (.Value = "ABCD" Or .Value = "EFGH" Or .Value = "IJKL") And .Offset(, 1).Value > 7 And Abs(.Offset(, 2).Value) > 1000 Then
            .EntireRow.Interior.ColorIndex = 6
        End If
    End With
Next i
End Sub
 
Upvote 0
I have got a Debug due to the headings in the first row.
What do I need to change in the code to start the checking form Row2?
 
Upvote 0
Just

Rich (BB code):
Sub fmt()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
        If (.Value = "ABCD" Or .Value = "EFGH" Or .Value = "IJKL") And .Offset(, 1).Value > 7 And Abs(.Offset(, 2).Value) > 1000 Then
            .EntireRow.Interior.ColorIndex = 6
        End If
    End With
Next i
End Sub
 
Upvote 0
I have got an additional question.
If I want to format all the other rows that have got different codes in Green, do I need to use something like "If (.Value <> "ABCD" or .Value <> "EFGH" ....?

I tried it but it doesn't work. Can you assist please?
 
Upvote 0
Try

Code:
Sub fmt()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        If (.Value = "ABCD" Or .Value = "EFGH" Or .Value = "IJKL") And .Offset(, 1).Value > 7 And Abs(.Offset(, 2).Value) > 1000 Then
            .EntireRow.Interior.ColorIndex = 6
        Else
            .EntireRow.Interior.ColorIndex = 4
        End If
    End With
Next i
End Sub
 
Upvote 0
Here is my code. I have 3 rules.

Code:
Sub fmt()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
        
If (.Value = "AAAB12" Or .Value = "AAAB16" Or .Value = "ABD12") And .Offset(, 14).Value > 30 Then
            .EntireRow.Interior.ColorIndex = 6
        End If

If (.Value <> "AAAB12" Or .Value <> "AAAB16" Or .Value <> "ABD12") And .Offset(, 14).Value > 7 And Abs(.Offset(, 8).Value) > 1000 Then
            .EntireRow.Interior.ColorIndex = 6
        End If

If (.Value <> "AAAB12" Or .Value <> "AAAB16" Or .Value <> "ABD12") And .Offset(, 14).Value > 90 Then
            .EntireRow.Interior.ColorIndex = 4
        End If

    End With
Next i
End Sub
 
Upvote 0
Try

Code:
Sub fmt()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
    With Range("A" & i)
                
        If (.Value = "AAAB12" Or .Value = "AAAB16" Or .Value = "ABD12") And .Offset(, 14).Value > 30 Then
                    .EntireRow.Interior.ColorIndex = 6
        Else
                .EntireRow.Interior.ColorIndex = 4
        End If
        
        If (.Value <> "AAAB12" Or .Value <> "AAAB16" Or .Value <> "ABD12") And .Offset(, 14).Value > 7 And Abs(.Offset(, 8).Value) > 1000 Then
                    .EntireRow.Interior.ColorIndex = 6
        Else
                .EntireRow.Interior.ColorIndex = 4
        End If
        
        If (.Value <> "AAAB12" Or .Value <> "AAAB16" Or .Value <> "ABD12") And .Offset(, 14).Value > 90 Then
                    .EntireRow.Interior.ColorIndex = 4
        Else
                .EntireRow.Interior.ColorIndex = 4
        End If
        
    End With
Next i
End Sub
 
Upvote 0
I appreciate your help however I cannot go for this solution because for the data that doesn't meet the rules it will have to be unformatted.

Any alternative?
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,879
Members
452,948
Latest member
Dupuhini

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