如何在 VBA 中使用 Excel 提示抛出错误
How to Throw Error with Excel Prompt in VBA
我有一些代码在两个单独的工作簿中查找具有给定 sheet 名称的值。
我想要做的是,当第一个工作簿没有 sheet 时,不会出现以下提示,而是 cancels/throws 一个错误并使用错误处理转到第二个传播sheet。我该怎么做?
目前我正在使用这段代码来实现:
fFormString1 = "'" & wkBookRef1 & firstShtName & "'!$L/1000"
fFormString2 = "'" & wkBookRef2 & firstShtName & "'!$L/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
注意:我希望在点差sheet 理想地关闭时执行此操作。或者视觉上不存在以提高我的客户的操作速度和流畅度。
ExecuteExcel4Macro
将 return 来自已关闭工作簿的值。如果工作表不存在,它将引发错误 1004 'A formula in this worksheet contains one or more invalid references.
ExternalWorksheetExists
使用它来测试工作表是否存在。
Function ExternalWorksheetExists(FilePath As String, FileName As String, WorksheetName As String) As Boolean
If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"
On Error Resume Next
Call ExecuteExcel4Macro("'" & FilePath & "[" & FileName & "]" & WorksheetName & "'!R3C3")
ExternalWorksheetExists = Err.Number = 0
On Error GoTo 0
End Function
使用 ExecuteExcel4Macro
时,所有引用必须作为 R1C1 字符串提供。以下是有效字符串的示例:
ExecuteExcel4Macro("'C:\Users\tinzina\Documents\[Book1.xlsm]Sheet1'!R6C12")
大量借鉴 Thomas 的回答(应得全分)。但是,这似乎对您不起作用。
使用 ExecuteExcel4Macro
但将值赋予变量 val
。然后检查这是否是您要查找的错误 Error(2023)
.
请查找以下代码:
'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
我有一些代码在两个单独的工作簿中查找具有给定 sheet 名称的值。
我想要做的是,当第一个工作簿没有 sheet 时,不会出现以下提示,而是 cancels/throws 一个错误并使用错误处理转到第二个传播sheet。我该怎么做?
目前我正在使用这段代码来实现:
fFormString1 = "'" & wkBookRef1 & firstShtName & "'!$L/1000"
fFormString2 = "'" & wkBookRef2 & firstShtName & "'!$L/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
注意:我希望在点差sheet 理想地关闭时执行此操作。或者视觉上不存在以提高我的客户的操作速度和流畅度。
ExecuteExcel4Macro
将 return 来自已关闭工作簿的值。如果工作表不存在,它将引发错误 1004 'A formula in this worksheet contains one or more invalid references.
ExternalWorksheetExists
使用它来测试工作表是否存在。
Function ExternalWorksheetExists(FilePath As String, FileName As String, WorksheetName As String) As Boolean
If Right(FilePath, 1) <> "\" Then FilePath = FilePath & "\"
On Error Resume Next
Call ExecuteExcel4Macro("'" & FilePath & "[" & FileName & "]" & WorksheetName & "'!R3C3")
ExternalWorksheetExists = Err.Number = 0
On Error GoTo 0
End Function
使用 ExecuteExcel4Macro
时,所有引用必须作为 R1C1 字符串提供。以下是有效字符串的示例:
ExecuteExcel4Macro("'C:\Users\tinzina\Documents\[Book1.xlsm]Sheet1'!R6C12")
大量借鉴 Thomas 的回答(应得全分)。但是,这似乎对您不起作用。
使用 ExecuteExcel4Macro
但将值赋予变量 val
。然后检查这是否是您要查找的错误 Error(2023)
.
请查找以下代码:
'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