|
用数据库吧,我用access试了一下,比较卡,用sqlite还可以,你可以试一试
模拟表格最大104万行
我这里的电脑建立数据库大概需要5分钟,查询的话就这些数据不超过3秒
数据库大概80MB 左右
Option Explicit
Dim CNN As New cConnection
Dim RS As New cRecordset
Dim SQL As String
Sub a()
Dim tim, TS
tim = Timer
TS = MsgBox("重新建立数据库", vbYesNo)
If TS = vbYes Then
Call 建立数据库
Else
Call 查询数据库
End If
Set CNN = Nothing
Set RS = Nothing
If Dir(ThisWorkbook.Path & "\a.db") = "" Then
MsgBox "没有建立数据库"
Exit Sub
End If
MsgBox Format(Timer - tim, "0.00")
End Sub
Sub 建立数据库()
Dim r As Range, I&, j&, S, BT, myfile, brr(), m&, s1$
If Dir(ThisWorkbook.Path & "\a.db") <> "" Then Kill ThisWorkbook.Path & "\a.db"
Application.ScreenUpdating = False
CNN.CreateNewDB ThisWorkbook.Path & "\a.db"
myfile = Dir(ThisWorkbook.Path & "\*.xlsb")
CNN.BeginTrans
Do While myfile <> ""
If myfile <> ThisWorkbook.Name Then
BT = ""
S = Replace(myfile, ".xlsb", "")
m = m + 1
ReDim Preserve brr(1 To m)
brr(m) = "表_" & S
Workbooks.Open ThisWorkbook.Path & "\" & myfile
Set r = [a1].CurrentRegion
For j = 1 To r.Columns.Count
BT = BT & "F" & j & ","
Next
BT = Left(BT, Len(BT) - 1)
SQL = "CREATE TABLE 表_" & S & "(ID INTEGER PRIMARY KEY AUTOINCREMENT," & BT & ")"
CNN.Execute SQL
For I = 1 To r.Rows.Count
s1 = ""
For j = 1 To r.Columns.Count
s1 = s1 & r.Cells(I, j) & ","
Next
s1 = Left(s1, Len(s1) - 1)
SQL = "INSERT INTO 表_" & S & "( " & BT & ") Values(" & s1 & ")"
CNN.Execute SQL
Next
Workbooks(myfile).Close , 0
End If
myfile = Dir
Loop
CNN.CommitTrans
Application.ScreenUpdating = False
End Sub
Sub 查询数据库()
Dim ARR, I&, S$, brr
Dim tow As Workbook
Application.ScreenUpdating = False
On Error Resume Next
Kill ThisWorkbook.Path & "\输出\*.*"
RmDir ThisWorkbook.Path & "\输出"
On Error GoTo 0
ARR = [a1].CurrentRegion
For I = 1 To UBound(ARR)
S = S & ARR(I, 1) & ","
Next
S = "(" & Left(S, Len(S) - 1) & ")"
CNN.OpenDB ThisWorkbook.Path & "\a.db"
SQL = "SELECT name FROM sqlite_master WHERE type='table' AND NAME LIKE '表%'"
RS.OpenRecordset SQL, CNN
brr = RS.GetRows
MkDir ThisWorkbook.Path & "\输出"
For I = 0 To UBound(brr, 2)
SQL = "select * from " & brr(0, I) & " where id in" & S
RS.OpenRecordset SQL, CNN
Set tow = Workbooks.Add
tow.Sheets(1).[a1].CopyFromRecordset RS.GetADORsFromContent
tow.Sheets(1).Range("a:a").Delete
tow.SaveAs ThisWorkbook.Path & "\输出\" & brr(0, I) & ".xlsb", FileFormat:=xlExcel12
tow.Close 1
Next
Application.ScreenUpdating = True
End Sub
|
|