0

我尝试使用 ADO 库制作与 sql 语句一起操作的宏,但实际上它只返回 6559 条记录,而我的一张表有 72k 条记录。为什么?

最近,我注意到,实际上我的代码并没有返回 6559,而是返回行数 - 65537。所以当我将工作表中的行数减少到 72092 时,我什至会得到更少的行数(6550)。

我注意到的另一件事是 rs.RecordCount 返回“-1”。

这是我的子程序的代码。它具有三个参数:sql 语句(sqlstmt)、目标工作表名称(sheet_name)和目标范围(destination1)。

'subprocedure that execute sql statements and save resault in given worksheet
    Public Sub sql_query(ByVal sqlstmt As String, ByVal sheet_name As String, ByVal destination1 As String)

        Dim conn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim connstring As String
        Dim qt As QueryTable
        Dim tw_path As String
        Dim is_name As Boolean
        Dim sh As Worksheet

        '''making sheet if it doesn't exist
        is_name = False
        For Each sh In ThisWorkbook.Worksheets
            If sh.Name = sheet_name Then is_name = True
        Next
        If is_name = False Then ThisWorkbook.Worksheets.Add(After:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)).Name = sheet_name

        ''' connection
        tw_path = ThisWorkbook.path & "\" & ThisWorkbook.Name
        connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & tw_path & ";Extended Properties=Excel 8.0;Persist Security Info=False"

        Set conn = New ADODB.Connection
        conn.ConnectionString = connstring
        conn.Open

        '''executing statement
        Set rs = New ADODB.Recordset
        rs.Source = sqlstmt
        rs.ActiveConnection = conn
        rs.Open

        '''saving records
        ThisWorkbook.Worksheets(sheet_name).Activate
        Set qt = Worksheets(sheet_name).QueryTables.Add(Connection:=rs, Destination:=Range(destination1))
        qt.Refresh

        '''end
        If rs.State <> adStateClosed Then rs.Close
        conn.Close
        If Not rs Is Nothing Then Set rs = Nothing
        If Not conn Is Nothing Then Set conn = Nothing
        Set qt = Nothing

        End Sub

感谢帮助

4

1 回答 1

0

我猜您使用的是 excel 2003 或以前的版本,在这种情况下,工作表最多有 65,536 行。我会把它放在评论而不是答案中,但我还差 1 个代表能够发表评论:(。对不起

于 2013-04-15T05:01:27.520 回答