|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
多表汇总.zip
(20.94 KB, 下载次数: 5)
Sub 案例5()
'菜单"工程"->"引用" 找 Microsoft ActiveX Data Object
Dim ws As Worksheet
Dim i As Integer
Set cnn = New ADODB.Connection
Set rs = New ADODB.Recordset
Set ws = ThisWorkbook.Worksheets("汇总")
ws.Cells.ClearContents
'建立与工作簿的连接
With cnn
.Provider = "microsoft.ace.oledb.12.0"
.ConnectionString = "Extended Properties=Excel 12.0;" _
& "Data Source=" & ThisWorkbook.FullName
.Open
End With
'进行统计汇总
Sql = "select '2月'as 月份,* from [2月$] union" _
& "select '3月'as 月份,* from [3月$] union" _
& "select'4月'as 月份,* from[4月$] union" _
& "select'5月' as 月份, *from[5月$] union" _
& "select'6月' as 月份,* from[6月$] union" _
& "select'7月' as 月份,* from [7月$]"
Set rs = New ADODB.Recordset
rs.Open Sql, cnn, adOpenKeyset, adLockOptimistic
ws.Range("A2").CopyFromRecordset rs
'关闭查询和连接
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
End Sub
|
|