|
问题交待的好像不是很清楚,下面代码是第一步,先看看是不是这样?
Private Sub CommandButton1_Click()
Dim Mypath$
Mypath = ThisWorkbook.Path & "\" & [p1]
If Dir(Mypath, vbDirectory) = "" Then
MsgBox "文件夹不存在,请检查!", vbInformation
End If
ActiveSheet.UsedRange.Offset(3).ClearContents
If [p1] = "周考" Then
Call 周考(Mypath)
ElseIf [p1] = "月考" Then
Call 月考(Mypath)
End If
End Sub
Sub 周考(Mypath$)
Dim MyFile$, SQL$, m&
Mypath = Mypath & "\"
MyFile = Dir(Mypath & "*.xls")
Set cnn = CreateObject("adodb.connection")
Do While Len(MyFile)
m = m + 1
If m = 1 Then
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & Mypath & MyFile
SQL = "select * from [sheet1$a4:n65536] where f2 is not null"
Else
SQL = SQL & " union all select * from [Excel 8.0;hdr=no;Database=" & Mypath & MyFile & "].[sheet1$a4:n65536] where f2 is not null"
End If
MyFile = Dir
Loop
[a4].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End Sub
Sub 月考(Mypath$)
Dim MyFile$, SQL$, m&, s$
Mypath = Mypath & "\"
MyFile = Dir(Mypath & "*.xls")
Set cnn = CreateObject("adodb.connection")
cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no';data source=" & Mypath & "一卷机读卡.xls"
s = "select * from [sheet1$a2:h65536] where f2 is not null"
Do While Len(MyFile)
If MyFile <> "一卷机读卡.xls" Then
m = m + 1
If m = 1 Then
SQL = "select * from [Excel 8.0;hdr=no;Database=" & Mypath & MyFile & "].[sheet1$a4:h65536] where f2 is not null"
Else
SQL = SQL & " union all select * from [Excel 8.0;hdr=no;Database=" & Mypath & MyFile & "].[sheet1$a4:h65536] where f2 is not null"
End If
End If
MyFile = Dir
Loop
SQL = "select a.f1,a.f2,b.f3,a.f3,b.f4,a.f4,b.f5,a.f5,b.f6,a.f6,b.f7,a.f7,b.f8,a.f8 from (" & SQL & ") a left join (" & s & ") b on a.f2=b.f2"
[a4].CopyFromRecordset cnn.Execute(SQL)
cnn.Close
Set cnn = Nothing
End Sub
[ 本帖最后由 zhaogang1960 于 2011-7-2 14:19 编辑 ] |
|