的问题是,你有一个额外的“(”式中,删除它,错误将会消失。 以下三贵代码,第一种变体,变体,我的观点,更具有可读性 (在当你只需要值,那么最好使用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
定义“不工作”。 – ApplePie