|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub test()
- Dim d As New Dictionary
- Dim cnn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim sql$, mybook$, mysheet$, mypath$, wjm$
- Dim arr
- Dim wb As Workbook
- Dim ws0 As Worksheet
- Dim ws As Worksheet
- Dim i%, r0%, r%
-
- Application.DisplayAlerts = False
- mybook = ThisWorkbook.FullName
-
- With cnn
- .Provider = "microsoft.jet.oledb.4.0"
- .ConnectionString = "extended properties=""excel 8.0;HDR=YES;IMEX=1"";data source=" & mybook
- .Open
- End With
-
- mypath = ThisWorkbook.Path & ""
- wjm = Dir(mypath & "*.xls")
-
- Do While wjm <> ""
- If wjm <> "汇总数据.xls" Then
- d(wjm) = ""
- End If
- wjm = Dir()
- Loop
-
- kk = d.Keys
- d.RemoveAll
-
- For i = 0 To UBound(kk)
- sql = "select * from [Excel 8.0;database=" & mypath & kk(i) & "].[sheet1$]"
- rs.Open sql, cnn, adOpenKeyset, adLockOptimistic
- For j = 0 To rs.Fields.Count - 1
- d(rs.Fields(j).Name) = ""
- Next
- rs.Close
- Next
-
- For Each aa In d.Keys
- If aa Like "F#" Or aa Like "F##" Then
- d.Remove aa
- End If
- Next
-
- m = 1
- For Each aa In d.Keys
- d(aa) = m
- m = m + 1
- Next
-
- With ThisWorkbook.Worksheets("sheet1")
- .Cells.Delete
- .Range("a1").Resize(1, d.Count) = d.Keys
- End With
-
- Set ws0 = ThisWorkbook.Worksheets("sheet1")
- For i = 0 To UBound(kk)
- Set wb = GetObject(mypath & kk(i))
- With ws0
- r0 = .Range("a1").CurrentRegion.Rows.Count
- End With
- With wb
- With .Worksheets(1)
- c = .Cells(1, Columns.Count).End(xlToLeft).Column
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- For j = 1 To c
- .Cells(2, j).Resize(r - 1, 1).Copy ws0.Cells(r0 + 1, d(.Cells(1, j).Value))
- Next
- End With
- End With
- wb.Close
- Next
-
- cnn.Close
- Set fso = Nothing
- Set rs = Nothing
- Set cnn = Nothing
- Application.DisplayAlerts = True
- End Sub
复制代码 |
|