|
各位坛友:
小弟下面这段代码,可以实现自动汇总文件中所有工作簿所需的内容,也许对有些人有用,现把代码和事例分享出来,希望能给予有缘人些许帮助。
模块中代码如下:
Sub 汇总()
Dim sh As Worksheet
Dim cn As Object, rst As Object
Dim s1 As String, s2 As String, SQL As String
Dim i As Long, j As Long
Set cn = CreateObject("adodb.connection")
Set rst = CreateObject("ADODB.Recordset")
Cells(2, 1).Resize(10000, 200).ClearContents
s1 = Dir(ThisWorkbook.Path & "\*.xls")
Do While s1 <> ""
If s1 <> "Jumper装箱单列表.xls" Then
cn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;';data source= " & ThisWorkbook.Path & "\" & s1
j = Cells(Rows.Count, 2).End(xlUp).Row + 1
SQL = "select * from [进出口$a15:u200]"
Cells(j, 1).CopyFromRecordset cn.Execute(SQL)
cn.Close
End If
s1 = Dir
Loop
End Sub
thisworkbook中代码如下:
Private Sub Workbook_Open()
Call 汇总
Dim i As Long
Cells(1, "u").Select
Cells(1, 1).Resize(50000, 21).Sort key1:=Range("u1"), key2:=Range("t1"), Header:=xlYes
End Sub
|
评分
-
1
查看全部评分
-
|