【问题标题】:VLOOKUP INSIDE A LOOP (FOR) VISUAL BASIC EXCEL循环内的 VLOOKUP (FOR) VISUAL BASIC EXCEL
【发布时间】:2017-05-29 02:16:15
【问题描述】:

我有以下问题 我有一个想要循环运行的 VLOOKUP,但是当 vlookup 没有找到相应的值时,它会停止脚本。如果我使用错误处理程序处理错误,它会跳转并停止它。

Sub Botón1_Haga_clic_en()
Dim filas As Integer
Dim desdefila As Integer

filas = InputBox("Cuantas files tiene éste bloque de pagos?")
desdefila = InputBox("Desde que fila empieza?")
filasfinal = filas + desdefila

For x = desdefila To filasfinal

Dim Nombre As String
Dim Rango As Range

Set Rango = Sheets(6).Range("A:B")

Nombre = Application.WorksheetFunction.VLookup(Range("A" & desdefila).Value, Rango, 2, 0)
Range("E" & desdefila).Value = Nombre
desdefila = desdefila + 1

Next

End Sub

关于如何返回循环或处理此错误的任何想法?

【问题讨论】:

  • 您将 Nombre 声明为字符串,因此如果找不到该值,它将返回 Excel 不喜欢的错误,因此将其声明为变体。然后在继续之前添加一个 Iferror 检查。
  • 你也可以分享你的错误处理程序吗?
  • 拜托,不要对人大喊大叫。
  • 您在下面得到了 rror 处理程序答案的答案。为了更好的编码(更快),在 For x = desdefila To filasfinal 循环之前使用 3 行 Dim Nombre As String 、 Dim Rango As Range 和 Set Rango = Sheets(6).Range("A:B") ,无需在每次进入循环时根据您的情况定义和设置范围

标签: vba excel for-loop vlookup


【解决方案1】:

您可以使用以下结构处理 Vlookup 错误:

Dim myValue As Variant

myValue = Application.VLookup("Some Value", Range("A:B"), 2, False)

If IsError(myValue) Then
    'Code for when not found
Else
    'Code for when found
End If

请注意,这不使用Application.WorksheetFunction.VLookup,而是使用Application.VLookup。

因此,对于您的代码,错误处理将被插入,如下所示:

Dim Nombre As Variant
Dim Rango As Range

Set Rango = Sheets(6).Range("A:B")

Nombre = Application.VLookup(Range("A" & desdefila).Value, Rango, 2, 0)

If IsError(Nombre) Then
    'Code for when not found
Else
    Range("E" & desdefila).Value = Nombre
End If

desdefila = desdefila + 1

【讨论】:

    【解决方案2】:

    如果查找失败,WorksheetFunction 中的早期绑定 VLookup 函数将引发运行时错误。

    如果查找失败是一个异常情况,并且您想干净利落地处理它,则需要一个错误处理子例程:

        On Error GoTo ErrHandler
    
        For ...
    
            Nombre = Application.WorksheetFunction.VLookup(Range("A" & desdefila).Value, Rango, 2, 0)
    
            '...
        Next
    
        Exit Sub
    ErrHandler:
        ' execution jumps here in case of a failed lookup
        MsgBox "Lookup of '" & Range("A" & desdefila) & "' failed. Please verify data."
        'if you want to resume the loop, you can do this:
        Resume Next
        'otherwise execution of the procedure ends here.
    End Sub
    

    如果您知道查找失败的可能性,但不是很异常,并且您只想处理它并继续前进,您可以使用On Error Resume Next / On Error GoTo 0 隐藏错误而是:

        On Error Resume Next
        Nombre = Application.WorksheetFunction.VLookup(Range("A" & desdefila).Value, Rango, 2, 0)
        If Err.Number <> 0 Then Nombre = "Not Available"
        Err.Clear
        On Error GoTo 0
    

    或者,您可以使用扩展Application 接口的后期绑定版本(如elmer007's answer);当查找失败时,它不会引发 VBA 运行时错误,而是返回一个 错误值,您可以使用 IsError 函数检查该值:

    Dim Nombre As Variant 'note "As Variant"
    
    For ...
    
        Nombre = Application.VLookup(Range("A" & desdefila).Value, Rango, 2, 0)
        If IsError(Nombre) Then
            'handle error value
        End If
    
        '...
    
    Next
    

    使用早期绑定版本的优点之一是您可以获得 IntelliSense 参数,而使用后期绑定版本时您需要确切知道自己在做什么。

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2011-10-29
      • 1970-01-01
      相关资源
      最近更新 更多