vba - Vlookup 和 ISERROR

标签 vba error-handling excel-formula vlookup powerpoint

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

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

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

最佳答案

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

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

编写 native 工作表 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 中接收值的最佳方式。

udf_vlookup

关于vba - Vlookup 和 ISERROR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664998/

相关文章:

vba - 插入工作表

asp.net-mvc - 使用 IOCControllerFactory 时,像 StackOverflow 一样抛出 404,无需重定向

excel - 在 Excel 中查找组合并计算它们

excel-formula - 复制格式化文本并在 Excel 2007 中存储为单元格值

excel - 将源工作表复制到源工作表名称更改的另一个工作簿

excel - 无法在中断模式下执行宏

excel - 如何在excel vba中删除特定列(不完全)

ms-access - 如何在 "The runCommand action was canceled"语句后删除讨厌的 "cancel = true"对话框

error-handling - 在Silex中使用错误处理程序时,如何渲染 Twig 模板?

excel - 如何在没有VBA或宏的情况下在Excel中循环?