Excel / ADO:访问logging集时出错

我通常可以运行下面的代码而没有问题,但有时候在logging集中找不到任何logging。 文件本身很大 – 超过200k行。 此外,它有一些合并的单元格,列宽不够宽,无法自动拟合查看数据(我不确定是否可能是一个促成因素)。 我还想补充一点,没有logging被发现的时间主要在运行Excel 2010的计算机上,而成功的实例已经在运行Excel 2013的计算机上。

这是我的代码:

Dim oConn As New ADODB.Connection Dim oRS As New ADODB.Recordset Dim sPath Dim sSQL As String Dim fd As Office.FileDialog Dim fr11 As String Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "FR11". sSQL = "select F3,F6,F8,F9,F10,F18,F22,F23,F28 from [Natural Detail $] where F18 = '0000121046' or F25 = 'Natural GL Acct Nbr'" Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .AllowMultiSelect = False .Title = "Please select the file." box. .Filters.Clear .Filters.Add "Excel", "*.xlsx" .Filters.Add "All Files", "*.*" If .Show = True Then fr11 = .SelectedItems(1) End If End With DBPath = fr11 oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DBPath & "';" & _ "Extended Properties='Excel 12.0 Xml;HDR=No;IMEX=1;MaxScanRows=0';" oRS.Open sSQL, oConn If Not (oRS.BOF And oRS.EOF) Then Worksheets("FR11").Range("A1").CopyFromRecordset oRS Else MsgBox "No records found" End If oRS.Close oConn.Close Set oConn = Nothing 

有没有什么你可能会导致上述有时失败,或者这将改善一般的代码。 任何反馈非常感谢。

这是一个数据的样本:

 Company Name Company Name Company Info Accounting Date: 04/12/2016 Company Code Company Code Type Group TP RES Band Name Arrival Date Departure Date ACCTING CENTER1 ACCTING CENTER2 Center Name GL AL Tran Type Name Acctng Tran Type Name Natural Posting Item Name Creation User Id SAP Account Name Paymnet Method Document Number Payment Originator Name Natural Posting Amt ID Acctng Tran Id Natural GL Acct Nbr Natural CAC Id PAY ID ID Totals 222 7887878 Master 4696941 0 4696941 random name 04/09/2016 04/23/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 5857 0000121046 4 1616165 649848 777 7768 Master 7575 0 783783 random name 12/01/2015 02/26/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 8778 0000121046 5 6168161 128572150 783783 4696941 Master 4696941 0 783783 random name 04/09/2016 04/25/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 8 0000121046 7 198816313 5464 4696941 78666 Master 4696941 0 4696941 random name 04/10/2016 04/22/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 1097152750 0000121046 3 171984 5616 78 4696941 Master 786 0 783783 random name 02/19/2016 03/04/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 27217 0000121046 1 515678 115616 66 786 Master 4696941 0 78378 random name 04/02/2016 04/06/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 4177 0000121046 2 56468 117980742 22 666/// Master 4696941 0 42753 random name 04/09/2016 04/29/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 9 0000121046 32 198805200 42742 783 86788 Master 4696941 0 4696941 random name 04/01/2016 04/17/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 879 0000121046 7 254948 1561 66676 4696941 Master 4696941 0 4696941 random name 02/29/2016 03/15/2016 Undistributed Undistributed Company Info REC Ledger RE Ledger CC ******* random name 4696941 4696941 78 0000121046 7 45618 615 

在语法上,你的VBA代码没有什么问题。 只是你的SQL查询的WHERE条件不会返回logging,因此它为什么零星工作。 对于已发布的示例数据,F18和F25在列中没有如下所示的WHERE子句值:

电子表格输入

如果您删除WHERE条件,则返回所有logging:

电子表格输出

工作表可能会有不同的列安排,所以F18和F25可能指向不同的领域。 因此,请考虑显式引用列以及包含logging集的工作表部分。 确保每个工作表中都存在这样的列,否则查询将失败。 最后,在连接string中指定HDR=Yes以指示第一行包含列名称:

 sSQL = "select [Company1], [Group], [RES], [Band], [Name1]," _ & " [CENTER2], [AL], [Tran], [Type2]" _ & " from [Natural Detail$A10:BL19]" _ & " where [CENTER2]='0000121046' or [Name2]='Natural GL Acct Nbr'" ... oConn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DBpath & "';" & _ "Extended Properties=""Excel 12.0 Xml;HDR=Yes;"";" 

您将会注意到,上面的SQL语句通过一些列名称对后缀进行了编号,这些列名称是由于数据源中重复的列名称(公司,代码,types,名称等)而引起的,Jet / ACE SQL Engine后缀为数字logging集,因为每个字段必须唯一标识。 一次没有号码,每一个后面加上后缀数字:公司,公司1,公司2 …还要注意:与HDR=Yes ,没有字段名称将导致logging集:

电子表格输出没有标题

最后但并非最不重要的是,对于最佳实践总是error handling您的代码,以显示运行时错误的显式消息,并在全局空间中使用Option Explicit来确保正确声明variables/对象:

 Option Explicit Sub RunSQL() On Error GoTo ErrHandle Dim oConn As New ADODB.Connection Dim oRS As New ADODB.Recordset ... oRS.Close oConn.Close Set oConn = Nothing Exit Sub ErrHandle: MsgBox Err.Number & " - " & Err.Description, vbCritical, "RUNTIME ERROR" Exit Sub End Sub