lundi 5 février 2018

Vlookup in two different areas across a column

I have a worksheet (Sheet 1) that contains a help column AR that contains the number "5" or text "Invalid". I want column AS to do a specific vlookup if AR contains the number "5", but if it contains the text "invalid" to do a separate specific vlookup. Currently what I have just overwrites to what is done in the else section of my loop to be the last iteration over the column and just ends up doing a vlookup for one or the other. I'm using column Y in sheet 1 as the specific value (aCell) that is being used to vlookup. Any help would go a long way, thanks!

Dim wsThis As Worksheet
Dim aCell As Range
Set wsThis = Sheets("Sheet3")
Dim wsAnd As Worksheet
Set wsAnd = Sheets("Sheet2")
Dim LastRow As Long, myRng As Range
LastRow = Sheets("Sheet3").UsedRange.Rows.Count
With wsIt
    For x = 2 To LastRow
        If Sheets("Sheet1").Range("$AR$" & x) = "4" Then
            For Each aCell In wsIt.Range("Y2:Y" & LastRow)
                .Cells(aCell.Row, 45) = "Not Found"
                On Error Resume Next
                .Cells(aCell.Row, 45) = Application.WorksheetFunction.VLookup( _
                                aCell.Value, wsThis.Range("$B$2:$Q$400"), 5, False)
                On Error GoTo 0
            Next aCell
        End If
    Next
End With

With wsIt
    For x = 2 To LastRow
        If Sheets("Sheet1").Range("$AR$" & x) = "EFI/RF" Then
            For Each aCell In wsIt.Range("Y2:Y" & LastRow)
                .Cells(aCell.Row, 45) = "Not Found"
                On Error Resume Next
                .Cells(aCell.Row, 45) = Application.WorksheetFunction.VLookup( _
                                aCell.Value, wsAnd.Range("$B$2:$Q$400"), 5, False)
                On Error GoTo 0
            Next aCell
        End If
    Next
End With

Aucun commentaire:

Enregistrer un commentaire