详细的QueryTableerror handling

下午好,

我正在寻找一种方法来处理QueryTable错误。 我已经看了谷歌和stackoverflow上的其他问题,但是他们似乎并没有回答我想问的具体问题。

基本上,有没有办法确定处理QueryTables错误时的具体错误?

码:

On Error Goto queryError With Activesheet.QueryTables... .... End With queryError: Msgbox("There was an error with the QueryTables. The reason for the error was: " & myError) 

有没有办法将myError设置为给出更具体的问题是什么,即使这意味着select某种状态码? 例如

 QueryTables.StatusCode... 

或者其他的东西?

提前致谢。

如何处理错误:
Excel VBA不支持Try Catch Finally 。 相反,它使用On Error GoTo

要完全控制Excel中的error handling,您必须使用标签(始终以冒号结尾)。

在这个例子中,这两个标签是:

  • 再试一次:
  • queryError:

假设正在创build的查询表是来自一个文本文件,如下所示:

文本文件

当您第一次运行例程时,会提示用户input三个input:

  • 文件path
  • 新表名
  • 单元格(即范围)粘贴到

输入框

如果这些input中的任何一个发生错误,代码将立即转到标签queryError:

所以说,用户没有input一个有效的文件path,它看起来像这样:

错误信息

如果用户点击是(再次尝试),则Resume tryAgain将代码备份到该标签并遍历所有。

最后请注意Select Case 。 这就是你可以如何控制你想如何处理特定的错误。

这里是粘贴在模块中的代码:

 Option Explicit Sub CreateQueryTable() 'Assign values to these variables by prompting user with Input Boxes Dim filepath As String Dim qryTableName As String Dim startCellForTable As Range 'These variables are used in the error handling prompts Dim Msg As String Dim Ans As Integer 'If an error occurs, code will go to the label `queryError:` On Error GoTo queryError tryAgain: 'Prompt user for the filename of the .txt file to use as QueryTable Source filepath = InputBox("Please enter filepath of text file to use as the source") 'Prompt user to name the new Query Table qryTableName = InputBox("Please enter name of Query Table") 'Prompt user for the cell to put table at Set startCellForTable = Application.InputBox(Prompt:="Please select a cell where you would like to paste the table to", Type:=8) 'If user hits OK, check to see that they at least put something as a value If filepath <> "" And qryTableName <> "" And startCellForTable <> "" Then 'format filepath variable so can pass it as argument to QueryTables.Add 'Trim any leading or trailing spaces from qryTableName filepath = "TEXT;" & filepath qryTableName = Trim(qryTableName) End If 'Create QueryTable at Range("A1") With ActiveSheet.QueryTables.Add(Connection:=filepath, Destination:=Range(startCellForTable.Address)) .Name = qryTableName .Refresh BackgroundQuery:=False End With 'If there are no errors, exit the procedure (so the `queryError:` code won't execute) Exit Sub queryError: Msg = "" 'Say that an error occured Msg = Msg & "An error occurred with the Query Table. " & vbNewLine & vbNewLine 'Use Excel's built-in Error object (named `Err`) to show error number and description of error Msg = Msg & Err.Number & ": " & Error(Err.Number) & vbNewLine & vbNewLine Select Case Err.Number 'Type mismatch Case 13 'Object required Case 424 Msg = Msg & vbNewLine & "Please check that a valid range was selected" & vbNewLine 'Application defined or Object defined error Case 1004 Msg = Msg & vbNewLine & "Please check that this filepath is correct: " & vbNewLine & vbNewLine & filepath & vbNewLine Case Else End Select 'Prompt user to Try Again Msg = Msg & vbNewLine & vbNewLine & "Try again?" Ans = MsgBox(Msg, vbYesNo + vbCritical) 'If user says Yes, clear the error, and resume execution of code at label `TryAgain:` If Ans = vbYes Then Resume tryAgain End Sub