Add additional condition to an existing Macro.

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have the following Macro which works flawlessly in hi-lighting a row if it contains a specific word or phrase.

VBA Code:
Sub Local_BACKGROUND()
    Sheets("72 Hr").Select
    endrow = Range("E" & Rows.Count).End(xlUp).Row

For Each cell In Range("E3:E" & endrow)
  If cell.Value = "LOCAL" Then
     Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 6
  End If
Next

End Sub

I would like the row to highlight if there is a different value in it. I tried simply as another piece to it, but it errored out on me. The piece I was trying to add was

VBA Code:
If cell.Value = "CUST & AG REQ" Then
Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 44

I ended up with this code (below) which of course did not work. I could created a who new Macro but I have too many in this projec already. Thank You .

VBA Code:
Sub Local_BACKGROUND()
Sheets("72 Hr").Select
endrow = Range("E" & Rows.Count).End(xlUp).Row
For Each cell In Range("E3:E" & endrow)
If cell.Value = "LOCAL" Then
Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 6
For Each cell In Range("E3:E" & endrow)
If cell.Value = "CUST & AG REQ" Then
Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 44
End If
Next
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Maybe something like this?

VBA Code:
Sub Local_BACKGROUND()
    Sheets("72 Hr").Select
    endrow = Range("E" & Rows.Count).End(xlUp).Row
    
    With Range(Cells(cell.Row, "A"), Cells(cell.Row, "E"))
        For Each cell In Range("E3:E" & endrow)
            If cell.Value = "LOCAL" Then
                .Interior.ColorIndex = 6
            End If
            
            If cell.Value = "CUST & AG REQ" Then
                .Interior.ColorIndex = 44
            End If
        Next cell
    End With
End Sub
 
Upvote 0
Thank you so much for the quick response. Unfortunately, I got an error , so I think you're very near it. Below is the message and what hilighted once I selected the "debug" button.
macro update.PNG
 
Upvote 0
Sorry, stupid mistake on my part. I should have paid better attention Try this.

VBA Code:
Sub Local_BACKGROUND()
    Sheets("72 Hr").Select
    endrow = Range("E" & Rows.Count).End(xlUp).Row
    
    For Each cell In Range("E3:E" & endrow)
        If cell.Value = "LOCAL" Then
            Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 6
        End If
        If cell.Value = "CUST & AG REQ" Then
            Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 44
        End If
    Next cell
End Sub
 
Upvote 0
Sorry, stupid mistake on my part. I should have paid better attention Try this.

VBA Code:
Sub Local_BACKGROUND()
    Sheets("72 Hr").Select
    endrow = Range("E" & Rows.Count).End(xlUp).Row
   
    For Each cell In Range("E3:E" & endrow)
        If cell.Value = "LOCAL" Then
            Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 6
        End If
        If cell.Value = "CUST & AG REQ" Then
            Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 44
        End If
    Next cell
End Sub
That works brilliantly, there is just one thing. Sometimes I may have multiple words with "Local" or "Cust & AG REQ", for example "LOCAL // EXPECT TO STEP" or "CUST & AG REQ // CARGO ACFT // EXPECT TO STEP". Isn't there something I can use so if it picks any of those two group of words that it will highlight accordingly. I thought inputting "CASE" somewhere in the formula may help. Thank you so much, you have gotten me so much closer, now I just need to be dragged across the finish line.
 
Upvote 0
If you change to use 'Select Case' you will be able to use a word list.
VBA Code:
Sub Local_BACKGROUND()
    Sheets("72 Hr").Select
    endrow = Range("E" & Rows.Count).End(xlUp).Row
    
    For Each cell In Range("E3:E" & endrow)
        Select Case cell.Value
            Case "LOCAL", "LOCAL // EXPECT TO STEP", "Dog", "Ice Cream"
                Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 6
            Case "CUST & AG REQ", "CUST & AG REQ // CARGO ACFT // EXPECT TO STEP"
                Range(Cells(cell.Row, "A"), Cells(cell.Row, "E")).Interior.ColorIndex = 44
        End Select
    Next cell
End Su
b
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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