2015-01-11 91 views
0

有一个代码,我一直在考虑如何调整IfError for vlookup函数找不到的值。要求是将公式转换为Excel电子表格。你知道为什么它不起作用吗?谢谢你,拉斯VBA vlookup iferror公式错误'1004'

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 
+1

定义“不工作”。 – ApplePie

回答

1

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

太谢谢你了,瓦西里!我真的很感激及时和详细的解答。特别感谢3个版本!!! Off topic:你来自俄罗斯吗?保重, – LearnForever

+0

太棒了!!!可以给你发个ap内含消息? – LearnForever