Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA中用Excel提示抛出错误_Excel_Error Handling_Excel Addins_Vba - Fatal编程技术网

如何在VBA中用Excel提示抛出错误

如何在VBA中用Excel提示抛出错误,excel,error-handling,excel-addins,vba,Excel,Error Handling,Excel Addins,Vba,我有一些代码在两个单独的工作簿中查找具有给定工作表名称的值 我想做的是,当第一个工作簿没有工作表时,它会取消/抛出一个错误,并使用错误处理转到第二个电子表格,而不是出现下面的提示。我该怎么做 目前我正在使用此代码来实现以下目标: fFormString1 = "'" & wkBookRef1 & firstShtName & "'!$L$6/1000" fFormString2 = "'" & wkBookRef2 & firstShtName &

我有一些代码在两个单独的工作簿中查找具有给定工作表名称的值

我想做的是,当第一个工作簿没有工作表时,它会取消/抛出一个错误,并使用错误处理转到第二个电子表格,而不是出现下面的提示。我该怎么做

目前我正在使用此代码来实现以下目标:

fFormString1 = "'" & wkBookRef1 & firstShtName & "'!$L$6/1000"
fFormString2 = "'" & wkBookRef2 & firstShtName & "'!$L$6/1000"
Application.DisplayAlerts = False 'Does nothing to the prompt
On Error GoTo tryTwo 'Following only throws error when prompt is canceled
    ThisWorkbook.Sheets("Place").Range("E53").Formula = "=" & fFormString1
    GoTo endTen

tryTwo:
    ThisWorkbook.Sheets("Place").Range("E53").Formula = "=IFERROR(" & fFormString2 & ","""")"
    On Error Resume Next
endTen:
Application.DisplayAlerts = True 'Does nothing to the prompt
注:我希望在电子表格关闭的情况下进行此操作。或者不显示以提高客户端操作的速度和平滑度。

ExecuteExcel4Macro将从关闭的工作簿返回一个值。如果工作表不存在,它将抛出错误1004'此工作表中的公式包含一个或多个无效引用

ExternalWorksheetExists使用此选项测试工作表是否存在

ExecuteExcel4Macro将从关闭的工作簿返回一个值。如果工作表不存在,它将抛出错误1004'此工作表中的公式包含一个或多个无效引用

ExternalWorksheetExists使用此选项测试工作表是否存在


从托马斯的回答中大量借款,全部信用到期。然而,这似乎不适合你

使用ExecuteExcel4Macro,但将该值归因于变量val。然后检查这是否是您要查找的错误Error2023

请查找以下代码:

'Check if the sheet exists in the workbook, used to check which forecast file one should look in
Function ExtSheetExists(formString) As Boolean 'Form string is a formula string with both the worksheet and the workbook
    Dim val As Variant
    'Tries to execute formula and throws error if it doesn't exist
    On Error Resume Next
    val = ExecuteExcel4Macro(formString)
    ExtSheetExists = (val <> Error(2023)) 'Returns False if the sheet does not exist based on Error 2023
    On Error GoTo 0
End Function

从托马斯的回答中大量借款,全部信用到期。然而,这似乎不适合你

使用ExecuteExcel4Macro,但将该值归因于变量val。然后检查这是否是您要查找的错误Error2023

请查找以下代码:

'Check if the sheet exists in the workbook, used to check which forecast file one should look in
Function ExtSheetExists(formString) As Boolean 'Form string is a formula string with both the worksheet and the workbook
    Dim val As Variant
    'Tries to execute formula and throws error if it doesn't exist
    On Error Resume Next
    val = ExecuteExcel4Macro(formString)
    ExtSheetExists = (val <> Error(2023)) 'Returns False if the sheet does not exist based on Error 2023
    On Error GoTo 0
End Function

你有代码让我们看吗?@上面添加了共产国际代码,谢谢。你有代码让我们看吗?@上面添加了共产国际代码,谢谢。看来ExecuteExcel4Macro总是返回一个错误。是的,我已经检查了无数次文件路径。当非零1004I从输入字符串复制粘贴引用时,错误似乎总是返回,并且它工作得很好。不过,我没有使用相对RC引用。但当我这样做的时候,它总是返回真的。如果我使用非RC,它总是返回FALSE,我会在更新的答案中给出一个有效字符串的示例。ExecuteExcel4Macro只接受RC字符串。我不明白为什么在使用RC字符串时总是返回true。如果工作表不存在,ExecuteExcel4Macro将抛出错误。没有关系。ExecuteExcel4Macro将执行工作表函数、执行宏或从关闭或打开的工作簿返回范围值。感谢您的帮助。我找到了一个用val=代替Call的工作,它返回单元格的值或错误。那么我就用valerror2023作为逻辑条件。我应该编辑以反映这一点吗?ExecuteExcel4Macro似乎总是返回一个错误。是的,我已经检查了无数次文件路径。当非零1004I从输入字符串复制粘贴引用时,错误似乎总是返回,并且它工作得很好。不过,我没有使用相对RC引用。但当我这样做的时候,它总是返回真的。如果我使用非RC,它总是返回FALSE,我会在更新的答案中给出一个有效字符串的示例。ExecuteExcel4Macro只接受RC字符串。我不明白为什么在使用RC字符串时总是返回true。如果工作表不存在,ExecuteExcel4Macro将抛出错误。没有关系。ExecuteExcel4Macro将执行工作表函数、执行宏或从关闭或打开的工作簿返回范围值。感谢您的帮助。我找到了一个用val=代替Call的工作,它返回单元格的值或错误。那么我就用valerror2023作为逻辑条件。我应该编辑以反映这一点吗?
'Check if the sheet exists in the workbook, used to check which forecast file one should look in
Function ExtSheetExists(formString) As Boolean 'Form string is a formula string with both the worksheet and the workbook
    Dim val As Variant
    'Tries to execute formula and throws error if it doesn't exist
    On Error Resume Next
    val = ExecuteExcel4Macro(formString)
    ExtSheetExists = (val <> Error(2023)) 'Returns False if the sheet does not exist based on Error 2023
    On Error GoTo 0
End Function