|
要求越复杂,运行速度越慢,下面采用ADO加数组,不知道速度是否会提高:
- Sub ADO加数组法()
- t1 = Timer
- Dim cnn As Object, rs As Object, rst As Object, SQL$, Mypath$, MyFile$, s$, t$, a, arr, brr(1 To 150000, 7), i&, j&, m&, v$
- Application.ScreenUpdating = False
- Dim objWMI As Object
- Const HKEY_LOCAL_MACHINE = &H80000002
- Set objWMI = GetObject("winmgmts:\\.\root\default:StdRegProv")
- arr = Range("A2:F3")
- For i = 1 To UBound(arr, 2)
- If arr(2, i) <> "" Then
- If i = 1 Then
- t = t & " and " & arr(1, i) & "=#" & arr(2, i) & "#"
- Else
- t = t & " and " & arr(1, i) & "='" & arr(2, i) & "'"
- End If
- End If
- Next
- If t = "" Then Exit Sub
- t = Mid(t, 5)
- If Application.Version < 12 Then
- v = "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties='excel 8.0;imex=1';Data Source="
- objWMI.SetDWORDValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Jet\4.0\Engines\Excel", "TypeGuessRows", 0
- Else
- v = "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='excel 12.0;imex=1';Data Source="
- objWMI.SetDWORDValue HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Office" & Application.Version & "\Access Connectivity Engine\Engines\Excel", "TypeGuessRows", 0
- End If
- On Error Resume Next
- Mypath = ThisWorkbook.Path & ""
- MyFile = Dir(Mypath & "*.xls*")
- Do While MyFile <> ""
- If InStr(MyFile, ThisWorkbook.Name) = 0 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open v & Mypath & MyFile
- Set rs = cnn.OpenSchema(20)
- Do Until rs.EOF
- If rs.Fields("TABLE_TYPE") = "TABLE" Then
- s = Replace(rs("TABLE_NAME").Value, "'", "")
- If Right(s, 1) = "$" Then
- Set rst = cnn.Execute("[" & s & "a2:a]")
- If Err.Number = 0 Then
- If rst.Fields(0).Name = "成本域编码" Then
- SQL = "select * from [" & s & "a2:F5000] where" & t
- Set rst = cnn.Execute(SQL)
- If Not rst.EOF Then
- arr = rst.GetRows
- w = Replace(MyFile, ".xls", "")
- sh = Replace(s, "$", "")
- For i = 0 To UBound(arr, 2)
- m = m + 1
- For j = 0 To 5
- brr(m, j) = arr(j, i)
- Next
- brr(m, 6) = w
- brr(m, 7) = sh
- Next
- End If
- End If
- Else
- Err.Clear
- End If
- End If
- End If
- rs.MoveNext
- Loop
- End If
- MyFile = Dir()
- Loop
- Range("A6:H65536").ClearContents
- Range("A6").Resize(m, 8) = brr
- rs.Close
- Set rs = Nothing
- rst.Close
- Set rst = Nothing
- cnn.Close
- Set cnn = Nothing
- Application.ScreenUpdating = True
- MsgBox "耗时:" & Format(Timer - t1, "0.00") & "秒!" & Chr(10) & "共有“" & [a65536].End(3).Row - 5 & "”条记录!", vbInformation, "完工"
- End Sub
复制代码 |
|