vendredi 13 juillet 2018

Execute Vlookup if an IF condition is satisfied

I am trying to run a loop with vlookup, if an IF condition is met. I have written the code provided below but it gives an error in error in line 3 saying type mismatch. lastrow and ws1 are well defined as I have managed to run this code without the if condition. I am providing both the code that managed to run without the if condition and the code that does not run

CODE 1 (IT RUNS BUT DOES NOT HAVE IF CONDITION)

With ws1
    .Range("G2:G" & lastrow3).Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:I,4,FALSE),"""")"
    .Range("G2:G" & lastrow3).Value = .Range("G2:G" & lastrow3).Value
End With

CODE 2 (IT DOES NOT RUN)

With ws1
    For i = 2 To lastrow3
        If .Cells(i, "E").Value = 0 And .Cells(i, "F").Value = 0 Then
        .Cells(i, "G").Formula = "=IFERROR(VLOOKUP(C2,'[NOT OK.xlsx]Sheet1'!F:I,4,FALSE),"""")"
        End If
    Next i
End With

Aucun commentaire:

Enregistrer un commentaire