我一直在尝试调整代码,以便将vErrorup函数找不到值的IfError考虑在内。要求是将公式转换为Excel电子表格。你知道它为什么不起作用吗?谢谢你,Russ
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
答案 0 :(得分:1)
问题是你有一个额外的"("在公式中,删除它,错误将消失。 在您的代码的三个变体下面,在我看来,第一个变体更具可读性 (如果您只需要值,那么最好使用工作表函数)
'==================================================================
'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