VBA vlookup iferror 公式错误 '1004'

VBA vlookup iferror formula error '1004'

我一直在尝试调整一段代码,以考虑到 vlookup 函数未找到的值的 IfError。要求是将公式传输到 Excel 电子表格中。你知道为什么它不起作用吗?谢谢你,拉斯

Sub Vlookup_Condition_Formula()
    Dim rng As Range
    Dim i As Long

    Application.ScreenUpdating = False

    Worksheets("Summary").Activate

    'Identify the Destination location to start populating vlookuped values

    Range("C2").Activate

    With Worksheets("Summary").Cells
        Set rng = .Range("A1:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)

        For i = 2 To rng.Rows.Count
            rng.Cells(i, 3).Formula = "=IFERROR((VLookup(" & .Cells(i, 1). _
            Address & "," & "'" & Sheets("FinancePartnerList").Name _
            & "'!A:B," & "2, " & "False), ""Not in Exception List"")"

        Next

    End With

    Application.ScreenUpdating = True

End Sub

问题是公式中多了一个“(”,去掉它错误就会消失。 在您的代码的三个变体下方,在我看来,第一个变体更具可读性 (如果您只需要值,那么最好使用 worksheetfunction)

'==================================================================
'another one variant with same output result
'using worksheetfunction.vlookup (without formulas)
Sub Vlookup_Condition()
    Dim Rcnt&, i&, SourceRng$
    On Error Resume Next
    Application.ScreenUpdating = False
    Worksheets("Summary").Activate
    SourceRng = Range("A:B").Address
    With Worksheets("Summary")
    Rcnt = .Cells(Rows.Count, 1).End(xlUp).Row
        For i = 2 To Rcnt
            .Cells(i, 3).Value = WorksheetFunction.VLookup(.Cells(i, 1).Value, _
                                 Worksheets("FinancePartnerList").Range(SourceRng), 2, 0)
            If Err.Number > 0 Then
                .Cells(i, 3).Value = "Not in Exception List": Err.Clear
            End If
        Next
    End With
    Application.ScreenUpdating = True
End Sub
'==================================================================
'your updated variant
'using formulas
Sub Vlookup_Condition_Formula1()
    Dim Rcnt&, i&
    Application.ScreenUpdating = False
    Worksheets("Summary").Activate
    With Worksheets("Summary")
        Rcnt = Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 2 To Rcnt
            Cells(i, 3).Formula = "=IFERROR(VLookup(" & .Cells(i, 1).Address _
                                        & "," & "'" & Sheets("FinancePartnerList").Name & _
                                        "'!A:B," & "2, 0), ""Not in Exception List"")"
        Next
    End With
    Application.ScreenUpdating = True
End Sub
'==================================================================
'your updated variant
'using Evaluate(formulas)
Sub Vlookup_Condition_2()
    Dim Rcnt&, i&
    Application.ScreenUpdating = False
    Worksheets("Summary").Activate
    With Worksheets("Summary")
        Rcnt = Cells(.Rows.Count, 1).End(xlUp).Row
        For i = 2 To Rcnt
            Cells(i, 3).Value = Evaluate("=IFERROR(VLookup(" & .Cells(i, 1).Address _
                                        & "," & "'" & Sheets("FinancePartnerList").Name & _
                                        "'!A:B," & "2, 0), ""Not in Exception List"")")
        Next
    End With
    Application.ScreenUpdating = True
End Sub