|
经测试,ADO+数组法比ADO联合查询速度更快:- Sub ADO加数组()
- tt = Timer
- Dim cnn As Object, SQL$, Mypath$, MyName$, arr, brr(1 To 60000, -1 To 11), i&, j&, m&
- Application.ScreenUpdating = False
- Mypath = ThisWorkbook.Path & ""
- MyName = Dir(Mypath & "*.xls")
- Do While MyName <> ""
- If InStr(MyName, ThisWorkbook.Name) = 0 Then
- Set cnn = CreateObject("ADODB.Connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;hdr=no';Data Source=" & Mypath & MyName
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF '该Do循环将查找工作簿中的所有工作表,如果仅取一个工作表,请说明该表的特征
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- SQL = "select * from [" & s & "a3:l] where f2 is not null"
- arr = cnn.Execute(SQL).GetRows
- For i = 0 To UBound(arr, 2)
- m = m + 1
- brr(m, -1) = m
- For j = 0 To 11
- brr(m, j) = arr(j, i)
- Next
- Next
- End If
- End If
- rs.MoveNext
- Loop
- End If
- MyName = Dir()
- Loop
- [a1].CurrentRegion.Offset(2).ClearContents
- [a3].Resize(m, 13) = brr
- Cells(m + 3, 1) = "合计"
- Cells(m + 3, 7).Resize(, 7).FormulaR1C1 = "=SUM(R3C:R" & m + 2 & "C)"
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- MsgBox Timer - tt
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|