即使没有错误,Excel-VBA也不会将SAS表格复制到Excel表格中

我希望我的VBA-Excel代码能够读取一个SAS表格,并将这个SAS表格复制到一个Excel表格中。 有很多检查点。 例如,首先检查SAS表是否与If fileExists(sSasTable1)存在,如果存在则继续进行( fileexist是检查表的另一个函数)。

然后,我实现了errorObject ,以确保当发生错误时,vba程序会向我显示错误的详细信息。

当我检查debugging时,我可以看到,代码可以在第一个if-loopfindSAS表,它打开连接和logging集,但它不能执行rTarget.CopyFromRecordset rs应该复制SAS-表到Excel表。 然后它转到DisplayErrorInfo而不是rs.close 。 但它不会给出任何错误信息。

所以我不太明白 行rTarget.CopyFromRecordset rs有什么问题? 为什么不显示任何错误信息?

 Public Sub InsertSASFileIntoExcel() Dim YearMonth As String, SASOutput1 As String, SASOutputPath As String YearMonth = Sheets("Prognos").Cells(11, 2).Value SASOutputPath = "C:\\directories\output" SASOutput1 = "prognos_" & YearMonth Dim sSasTable1 As String: sSasTable1 = SASOutputPath & "\" & SASOutput1 & ".sas7bdat" If fileExists(sSasTable1) Then Dim rTarget1 As Range: Set rTarget1 = Sheets("Forecast_201509").Range("A1") Dim conn As New ADODB.Connection ' New ADODB.Connection Dim rs As New ADODB.Recordset Dim errorObject As ADODB.Error ' creates error-check object On Error GoTo DisplayErrorInfo ' Set initialization properties conn.Provider = "SAS.LocalProvider.1" conn.Open ' Open the Connection ' Open the Recordset rs.Open sSasTable1, conn, adOpenForwardOnly, adLockReadOnly, ADODB.adCmdTableDirect 'Populate the indata range with the contents of the SAS dataset rTarget.CopyFromRecordset rs ' This line is supposed to copy the table onto the range rTarget, which is Range("A1") in the worksheet. rs.Close ' Close the Recordset Set rs = Nothing conn.Close ' Close the Connection Set conn = Nothing ' In case of error, the code comes here below and it shows the description of the error. DisplayErrorInfo: For Each errorObject In rs.ActiveConnection.Errors Debug.Print "Description :"; errorObject.Description Debug.Print "Number:"; Hex(errorObject.Number) Next Else If giveWarning = True Then Call MsgBox("Warning", vbInformation, "The SAS-Table doesn't exist") End If End If End Sub ' Check if a file exists Function fileExists(s_directory As String) As Boolean Dim obj_fso As Object Set obj_fso = CreateObject("Scripting.FileSystemObject") fileExists = obj_fso.fileExists(s_directory) End Function