VBA : highlight not in and based on two parameters

Hello Experts,

I have the following code which allows me to:
Highlights values of ws1 located in column F which are not in column F of ws2.

I would like to customize the code in order to cover the following requirements:

1-Restrict/limit the "found" variable. With the current version of the code found take into account all the values of Column F. I would like to stock only the values of Column F which contains an specific value in Column A for example "SAP"

2-Add a second condition to   "If found Is Nothing" such as column A is not empty something like offset(,-5)<>""then...

Thank you again for your help!


Sub highlight_not_in()

Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("LOG_NOT_FR")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("LOG_FR_SAP")
Dim criteria As String
Dim found As Range
Dim i As Long
Dim LastRow As Long

Application.ScreenUpdating = False

Sheets("LOG_NOT_FR").Select

LastRow = Range("E65536").End(xlUp).Row

    For i = LastRow To 2 Step -1

   criteria = ws1.Cells(i, 6).Value

   On Error Resume Next

   Set found = ws2.Range("F2:F" & Rows.Count).Find(What:=criteria, LookAt:=xlWhole)

   On Error GoTo 0


   If found Is Nothing Then

     ws1.Cells(i, 6).EntireRow.Interior.ColorIndex = 6

   End If

  Next i


Application.ScreenUpdating = True

MsgBox ("Done)

End Sub

Open in new window

LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Do you have a workbook you can share so I can test a solution?
LD16Author Commented:
Hello Martin,

Please find attached the workbook with the current macro.
Log-CostC-F.xlsm
Martin LissOlder than dirtCommented:
I'm looking at it. I may have questions in a while.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Martin LissOlder than dirtCommented:
Add a second condition to   "If found Is Nothing" such as column A is not empty something like offset(,-5)<>""then...
I'm sorry but don't understand what that means.
Restrict/limit the "found" variable. With the current version of the code found take into account all the values of Column F. I would like to stock only the values of Column F which contains an specific value in Column A for example "SAP"

I think the following code does that. If looks for "SAP" in column "A" of LOG_FR_SAP and excludes that row. The only value that I see is 'SAP.TT'. Will it always be exactly 'SAP.TT"?
Sub highlight_not_in()


Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("LOG_NOT_FR")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("LOG_FR_SAP")
Dim criteria As String
Dim found As Range
Dim i As Long
Dim LastRow As Long
Dim LastRow_ws2 As Long
Dim lngRow As Long
Dim bFound As Boolean

Application.ScreenUpdating = False

With ws2
    .Select
    LastRow_ws2 = .Range("A65536").End(xlUp).Row
End With

Sheets("LOG_NOT_FR").Select

LastRow = Range("E65536").End(xlUp).Row


For i = LastRow To 2 Step -1

    criteria = ws1.Cells(i, 6).Value

    With ws2
        If InStr(1, .Cells(i, 1), "SAP") > 0 Then
            bFound = False
            For lngRow = 2 To LastRow_ws2
                If .Cells(lngRow, 6) = criteria Then
                    bFound = True
                    Exit For
                End If
            Next
            If Not bFound Then
                ws1.Cells(i, 6).EntireRow.Interior.ColorIndex = 6
            End If
        End If
    End With
Next i


Application.ScreenUpdating = True

MsgBox ("Highlight for GID not in for France SAP has been done")

End Sub

Open in new window

LD16Author Commented:
Hello Martin,

Thank you for your feedback.

1.My first comment concerns the column A of ws1 based on your proposal I think the following code can do this:
With ws1
        If  Cells(i, 1) = "" then
            bFound = False
End with

Let me know what do you think.

2. InStr(1, .Cells(i, 1), "SAP")  is fine with me as it takes into account a cell in column A which contains "SAP". Cells of column A can be different from "SAP.TT" I can have for ex "SAP.TS" or "TB.SAP".
Martin LissOlder than dirtCommented:
Try this and tell me if it does what you want.

Sub highlight_not_in()


Dim ws1 As Worksheet: Set ws1 = ActiveWorkbook.Sheets("LOG_NOT_FR")
Dim ws2 As Worksheet: Set ws2 = ActiveWorkbook.Sheets("LOG_FR_SAP")
Dim criteria As String
Dim found As Range
Dim i As Long
Dim LastRow As Long
Dim LastRow_ws2 As Long
Dim lngRow As Long
Dim bFound As Boolean

Application.ScreenUpdating = False

With ws2
    .Select
    LastRow_ws2 = .Range("A65536").End(xlUp).Row
End With

Sheets("LOG_NOT_FR").Select

LastRow = Range("E65536").End(xlUp).Row

For i = LastRow To 2 Step -1
    If Cells(i, 1) <> "" Then
        criteria = ws1.Cells(i, 6).Value
    
        With ws2
            If InStr(1, .Cells(i, 1), "SAP") > 0 Then
                bFound = False
                For lngRow = 2 To LastRow_ws2
                    If .Cells(lngRow, 6) = criteria Then
                        bFound = True
                        Exit For
                    End If
                Next
                If Not bFound Then
                    ws1.Cells(i, 6).EntireRow.Interior.ColorIndex 6
                End If
            End If
        End With
    End If
Next i

Application.ScreenUpdating = True

MsgBox ("Highlight for GID not in for France SAP has been done")

End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LD16Author Commented:
Thank you Martin.

I got an error message : "Run-time error '438': Object doesn't support this property or method for
 ws1.Cells(i, 6).EntireRow.Interior.ColorIndex 6.

Thank you again for your help.
Martin LissOlder than dirtCommented:
Sorry. There should be an equal sign before the last 6. In other words

ws1.Cells(i, 6).EntireRow.Interior.ColorIndex = 6
LD16Author Commented:
Perfect, I have tested it and everything works! Thank you very much for your help!
LD16Author Commented:
Excellent
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015
LD16Author Commented:
Thank you very much Martin. I will read them!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.