【问题标题】:Vlookup and ISERRORVlookup 和 ISERROR
【发布时间】:2016-12-04 12:51:38
【问题描述】:

我使用的是 Office 2007。我有一个 PowerPoint 宏,它使用 Excel 工作表来执行 vLookup。我为 vLookup 创建了一个公共函数。当所有值都正确提供时,它工作得很好。现在,我正在尝试为无法找到查找值的情况捕获错误。功能代码为:

Public Function v_lookup _
            (lookup_value As Variant, _
            table_array As Range, _
            col_index_num As Integer, _
            range_lookup As Boolean) _
            As String

Dim varResult           As Variant
Dim objExcelAppVL       As Object
Set objExcelAppVL = CreateObject("Excel.Application")
objExcelAppVL.Visible = False

varResult = objExcelAppVL.Application.WorksheetFunction.VLookup _
            (lookup_value, _
            table_array, _
            col_index_num, _
            range_lookup)
If IsError(varResult) Then varResult = ""
v_lookup = varResult

objExcelAppVL.Quit
Set objExcelAppVL = Nothing

End Function

我使用以下语句从主宏调用此函数:

varGatherNumber = v_lookup(varDateTime, Lit_Sched_Table_Lookup, 5, vbFalse)

此代码在没有错误时运行良好。问题是,当查找失败时,我被扔进 Debug 指向,

 varResult = objExcelAppVL.Application.WorksheetFunction.VLookup

.. 声明。当出现 vlookup 错误时,它永远不会到达 If IsError(varResult)... 语句。如何正确捕获 vLookup 错误?

【问题讨论】:

  • 您是否经常重复使用此函数来填充许多行(或列)?
  • 去掉.WorksheetFunction所以Application.Vlookup(...
  • 感谢@ScottCraner。删除 .WorksheetFunction 解决了我的问题。感谢您的帮助。
  • 是的@Jeeped。我多次调用这个函数,每次都传入不同的变量和查找列。

标签: vba error-handling excel-formula vlookup powerpoint


【解决方案1】:

WorksheetFunction object 不会将错误值传递回变体;它只会让他们窒息。使用不带 WorksheetFunction 的 Excel Application object 能够处理错误值。您已经创建了一个 Excel.Application 对象;使用它。

通过将对象变量声明设为静态,可以避免使用CreateObject function 重复调用构造(和破坏)应用程序对象。这在可能被复制到长列的 UDF 中特别有用。

编写本机工作表VLOOKUP function 以允许完整的列引用而不会受到惩罚;截断对 Worksheet.UsedRange 属性的完整列引用将有助于此功能。

Option Explicit

Public Function v_lookup(lookup_value As Variant, _
                         table_array As Range, _
                         col_index_num As Integer, _
                         Optional range_lookup As Boolean = False) As String

    Dim varResult           As Variant
    Static objExcelAppVL    As Object


    'only create the object if it doesn't exist
    If objExcelAppVL Is Nothing Then
        Set objExcelAppVL = CreateObject("Excel.Application")
        objExcelAppVL.Visible = False
    End If

    'restrict full column references to the worksheet's .UsedRange
    Set table_array = objExcelAppVL.Intersect(table_array.Parent.UsedRange, table_array)

    varResult = objExcelAppVL.VLookup(lookup_value, _
                                      table_array, _
                                      col_index_num, _
                                      range_lookup)

    If IsError(varResult) Then varResult = ""
    v_lookup = varResult

    'do not destruct static vars - they are reused on subsequent calls
    'objExcelAppVL.Quit
    'Set objExcelAppVL = Nothing

End Function

我看到您专门传回了一个字符串,因此数字和日期将是它们的文本等价物。我想这是在 PowerPoint 中接收值的最佳方式。

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多