Highlight cells based on criteria

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

My first question in awhile here.


So, my question is this...
If the cell in the column named "office" is not blank, and the cell in the column named "Location" does not say "America", highlight the cell in the "Location" column red.



A couple of things to note....

1. I would like the search to stop at the bottommost row with data in column header named "report" as this is always the longest column with contiguous data.

2. The columns may switch around, so I would like the searches to find the cells based on their column header name, not column position.

3. I would like all search criteria to be case iNsEnsiTIve.



Thanks much!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I think this code will do what you want:
Code:
Sub MyMacro()

    Dim cOff As Integer
    Dim cLoc As Integer
    Dim cRep As Integer
    Dim c As Integer
    Dim lr As Long
    Dim l As Long
    
    Application.ScreenUpdating = False
    
'   Find column references
    For c = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
        Select Case LCase(Cells(1, c))
            Case "office"
                cOff = c
            Case "location"
                cLoc = c
            Case "report"
                cRep = c
        End Select
    Next c
    
'   Find last row with data in report column
    lr = Cells(Rows.Count, cRep).End(xlUp).Row
    
'   Loop through all rows starting in row 2
    For r = 2 To lr
        If (Cells(r, cOff) <> "") And (LCase(Cells(r, cLoc)) <> "america") Then
            Cells(r, cLoc).Interior.Color = vbRed
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,237
Members
448,555
Latest member
RobertJones1986

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