简体   繁体   中英

How do you use an IF/THEN statement to do a VLookUp statement down a range taking into account the error code?

I am trying to do a VLookUp through an IF/THEN statement.

My Excel formula would be:

    =If(F2=G2,Today(),Vlookup(A2,[Range],15,FALSE))

When that would give me an "n/a" error, I would the use the following formula:

    =IF(VALUE(LEFT(C2,1))>8,N2+28,N2+14))"

I am trying to combine these in a macro and cannot get the syntax worked out.

Here is what I am 'trying' so far:

    Set WS2 = ThisWorkbook.Sheets("Sheet2")
    Set WS4 = ThisWorkbook.Sheets("Sheet1 (2)")

    For RowNumber = Range("M" & Rows.Count).End(xlUp).Row To 2 Step -1
    With WS2
        If Range("F" & RowNumber).Value = Range("G" & RowNumber).Value Then Range("O" & RowNumber) = (Now) Else Range("O" & RowNumber) = MyStringVar1 = Application.WorksheetFunction.VLookup(Range("A" & RowNumber), WS4.Range("A2:V5000").Value, 15, False)"
        On Error GoTo 0
        If MyStringVar1 = "" Then Range("O" & RowNumber).Value = Range("O" & RowNumber).Value = "=IF(VALUE(LEFT(Range("C" & RowNumber).Value,1))>8,Range("N" & RowNumber).Value+28,Range("N" & RowNumber).Value+14))" Else Range("O" & RowNumber).Value = MyStringVar1
    End With
    Next RowNumber

I suspect you don't need to resort to VBA. If you wrap your VLOOKUP inside an IFERROR formula, something like below, you may be able to manage just with cell formulas.

=IF(F2=G2,Today(),IFERROR(VLOOKUP(A2,$A$2:$V$5000,15,FALSE),IF(VALUE(LEFT(C2,1))>8,N2+28,N2+14)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM