|
今天学习了下,改进了下某老师的代码,不再循环各字段写入数据!- Public Sub BBBBBBBBBBB()
- 'On Error Resume Next
- Application.DisplayAlerts = False
- Application.ScreenUpdating = False
- Dim t As Date, ne$, rs%, i%, ii%, Name$, FName$, pa, sql, n%, m%, n2%, x%
- Dim arr(1 To 9999)
- Dim rst As ADODB.Recordset
- Dim cnn As ADODB.Connection
- ne = [c2]: i = 0: rs = 3
- t = Timer
- Name = ThisWorkbook.Name
- Range("a4:h" & [h65536].End(3).Row + 1).ClearContents
- FName = Dir(ThisWorkbook.Path & "\*.xls")
- Do Until FName = "" ' ThisWorkbook.Name
- If FName <> Name Then
- i = i + 1
- arr(i) = FName
- End If
- FName = Dir
- Loop
- For ii = 1 To i
- n = 0: m = 0
- Set cnn = New ADODB.Connection
- pa = ThisWorkbook.Path & "" & arr(ii)
- cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;HDR=YES';data source=" & pa
- For x = 1 To 3
- Set rst = New ADODB.Recordset
- sql = "select * from [" & x & "部门$A3:F] where 姓名 like '%" & ne & "%'"
- Err.Clear
- rst.Open sql, cnn, 1, 3
- With Sheet1
- n = .Cells(65536, 1).End(xlUp).Row + 1
- m = .UsedRange.Columns.Count
- .Cells(n, 1).CopyFromRecordset rst
- n2 = .Cells(65536, 1).End(xlUp).Row + 1
- If n <> n2 Then
- .Range(.Cells(n, m - 1), .Cells(n2 - 1, m - 1)) = Left(arr(ii), Len(arr(ii)) - 4)
- .Range(.Cells(n, m), .Cells(n2 - 1, m)) = x & "部门"
- End If
- End With
- Next
- Next
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "耗时:" & Format(Timer - t, "0.00") & "秒!" & Chr(10) & "共有“" & [a65536].End(3).Row - 3 & "”条记录!", vbInformation, "完工"
- End Sub
复制代码 |
|