VBA simplify a for Each loop

Hello Experts,

I use the following loop to complete sheet 2 based on specific conditions:

With Sheets("Sheet2").Select

For Each c In Range("D1", Range("D" & Rows.Count).End(xlUp))

If c.Value = "France SAP" And c.Offset(, -3).Value Like "* Projects*" Then c.Offset(, 12).Value = "FR_Grenoble_PTN"
If c.Value = "China SAP" And c.Offset(, -3).Value Like "* Projects*" Then c.Offset(, 12).Value = "CN_Shanghai_PTN"
If c.Value = "UE France" And c.Offset(, -3).Value Like "*Projects*" Then c.Offset(, 12).Value = "FR_SarreUnion_PTN"


Next c

Open in new window


Instead of having specific values in the code I would like to defined a sheet3 in order to automate the loops:ex

Values in Sheet3

ColumnA;ColumnB;ColumnC;
France SAP;Projects;FR_Grenoble_PTN
China SAP;Projects;CN_Shanghai_PTN
UE France;Projects;FR_SarreUnion_PTN


The revised code should takes into account the values of Sheet 3 something like this

If c.Value = Sheet3.Range(A1).value  And c.Offset(, -3).Value Like "*Sheet3.Range(A1).value*" Then c.Offset(, 12).Value = Sheet3.Range(A3).value
.......

Not sure that that the like will work with a variable.

Additionally I don't know if there is a way to simplify this loop because if I have multiple lines in Sheet 3 ex 20 I need to multiply my if by 20:

If c.Value = Sheet3.Range(A1).value  And c.Offset(, -3).Value Like "*Sheet3.Range(A2).value*" Then c.Offset(, 12).Value = Sheet3.Range(A3).value
If c.Value = Sheet3.Range(B1).value  And c.Offset(, -3).Value Like "*Sheet3.Range(B2).value*" Then c.Offset(, 12).Value = Sheet3.Range(B3).value
....
Thank you in advance for your feedback.
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.

MacroShadowCommented:
This syntax should work:
If Sheet3.Range(A1).Value And c.Offset(, -3).Value Like "*" & Sheet3.Range(A1).Value & "*" Then c.Offset(, 12).Value = Sheet3.Range(A3).Value

Open in new window

As far as simplifying the loop, you will need to check for each condition, so you will need 20 lines.
LD16Author Commented:
Hello MacroShadow,

In your code you putted:
If Sheet3.Range(A1).Value And c.Offset(, -3).Value Like "*" & Sheet3.Range(A1).Value & "*" Then c.Offset(, 12).Value = Sheet3.Range(A3).Value

Open in new window


The first part shouldn't be like this:

If [b]c.Value = Sheet3.Range(A1).Value[/b] And c.Offset(, -3).Value Like "*" & Sheet3.Range(A1).Value & "*" Then c.Offset(, 12).Value = Sheet3.Range(A3).Value

Open in new window


Regards,
MacroShadowCommented:
I stand corrected.
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.

Rory ArchibaldCommented:
Here's one way:
    Dim c                           As Range
    Dim vData
    Dim sCol(1 To 3)                As String
    Dim n                           As Long
    
    With Sheets("Sheet3")
        For n = 1 To 3
            sCol(n) = "'" & .Name & "'!" & .Range("A1").CurrentRegion.Columns(n).Address
        Next n
    End With
    
    With Sheets("Sheet2")

        For Each c In .Range("D1", .Range("D" & Rows.Count).End(xlUp)).Cells
            vData = .Evaluate("LOOKUP(2,1/(" & sCol(1) & "=" & c.Address & ")/isnumber(search(" & c.Offset(, -3).Address & "," & sCol(2) & "))," & sCol(3) & ")")
            If Not IsError(vData) Then c.Offset(, 12).Value = vData
        Next c
        
    End With

Open in new window


You could simplify by concatenating the two lookup values into one column.
LD16Author Commented:
Got It however the number "3" what does it means the number of loops to be done?

Why not define a variable as follows?

 Dim sh As Worksheet
    Set sh = ThisWorkbook.Sheets("Sheet3")
loopscount=sh.Range("A1048576").end(xlUp).row

Open in new window


Thank you in advance for your feedback.
Rory ArchibaldCommented:
The 3 relates to the 3 columns in your lookup table.
LD16Author Commented:
Ok, sorry got it.
LD16Author Commented:
@Rory Archibald

I have a question concerning the loop, instead of having an equal for the col(2) is there a way to have a like ""
In fact the script is working only for an equal.
Rory ArchibaldCommented:
It's not doing an equality check on that column - it checks for col(2) containing the contents of the cell in column A.
LD16Author Commented:
Strange. So in that case we can put upper case in both variables?
Rory ArchibaldCommented:
It's not case sensitive.
LD16Author Commented:
Hello,

Here is an example:

If I put "Rao" in column E of Config sheet the lookup doesn't work.

However if I put in column E of file Config "Ty.RAO" the same information of Column A of sheet Result it works.
lookup-files.xlsm
LD16Author Commented:
@Aikimark, could you please reopen the question?
Rory ArchibaldCommented:
There is no config sheet in that file, and the version of my code you have doesn't refer to either that sheet or the Result sheet so I have no idea what you are referring to. Can you be specific please?
LD16Author Commented:
Hello,

Here is a new version of the file with the required additional information. I just adapt your code to fit sheet 3.

Thank you again for your help.
lookup-files2.xlsm
Rory ArchibaldCommented:
Thanks. Try this version:

Sub lookup()

    Dim c                     As Range
    Dim vData
    Dim sCol(1 To 7)          As String
    Dim n                     As Long

    With Sheets("Sheet3")
        For n = 1 To 7
            sCol(n) = "'" & .Name & "'!" & .Range("A1").CurrentRegion.Columns(n).Address
        Next n
    End With

    With Sheets("Sheet2")

        For Each c In .Range("D1", .Range("D" & Rows.Count).End(xlUp)).Cells
            vData = .Evaluate("LOOKUP(2,1/(" & sCol(1) & "=" & c.Address & ")/isnumber(search(" & sCol(5) & "," & c.Offset(, -3).Address & "))," & sCol(4) & ")")
            If Not IsError(vData) Then c.Offset(, 12).Value = vData
        Next c

    End With
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:
It works! Thank you again I didn't realize that that the like was in (search(" & sCol(5) & ","
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.