|
楼主 |
发表于 2021-5-19 22:59
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
录个宏看下:
Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveWorkbook.Connections("蜀吴联盟").OLEDBConnection
.BackgroundQuery = True
.CommandText = Array( _
"select ""蜀国"" as 簿名, * from (select ""刘备"" as 表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm].[刘备$] union all select ""张飞"" as " _
, _
"表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm].[张飞$] union all select ""关羽"" as 表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm]." _
, _
"[关羽$]) union all" & Chr(13) & "" & Chr(10) & "select ""吴国"" as 簿名, * from (select ""周瑜"" as 表名, * from [G:\office练习\多簿多表联合透视\吴国.xlsm].[周瑜$] union a" _
, _
"ll select ""鲁肃"" as 表名, * from [G:\office练习\多簿多表联合透视\吴国.xlsm].[鲁肃$] union all select ""吕蒙"" as 表名, * from [G:\office练习\" _
, "多簿多表联合透视\吴国.xlsm].[吕蒙$])")
.CommandType = xlCmdTable
.Connection = Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=G:\office练习\多簿多表联合透视\蜀吴联盟.xlsm;Mode=Share Deny Write;Ext" _
, _
"ended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Eng" _
, _
"ine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB" _
, _
":New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on" _
, _
" Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False" _
)
.RefreshOnFileOpen = False
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = "G:\office练习\多簿多表联合透视\蜀吴联盟.xlsm"
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
.ServerFillColor = False
.ServerFontStyle = False
.ServerNumberFormat = False
.ServerTextColor = False
End With
With ActiveWorkbook.Connections("蜀吴联盟")
.Name = "蜀吴联盟"
.Description = ""
End With
Workbooks("蜀吴联盟.xlsm").Connections.Add "蜀吴联盟", "", Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=G:\office练习\多簿多表联合透视\蜀吴联盟.xlsm;Mode=Share Deny Write;Ext" _
, _
"ended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Jet OLEDB:Eng" _
, _
"ine Type=35;Jet OLEDB:Database Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB" _
, _
":New Database Password="""";Jet OLEDB:Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on" _
, _
" Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=False" _
), Array( _
"select ""蜀国"" as 簿名, * from (select ""刘备"" as 表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm].[刘备$] union all select ""张飞"" as " _
, _
"表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm].[张飞$] union all select ""关羽"" as 表名, * from [G:\office练习\多簿多表联合透视\蜀国.xlsm]." _
, _
"[关羽$]) union all" & Chr(13) & "" & Chr(10) & "select ""吴国"" as 簿名, * from (select ""周瑜"" as 表名, * from [G:\office练习\多簿多表联合透视\吴国.xlsm].[周瑜$] union a" _
, _
"ll select ""鲁肃"" as 表名, * from [G:\office练习\多簿多表联合透视\吴国.xlsm].[鲁肃$] union all select ""吕蒙"" as 表名, * from [G:\office练习\" _
, "多簿多表联合透视\吴国.xlsm].[吕蒙$])"), 3
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("蜀吴联盟"), Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:="分类汇总!R1C1", TableName:="数据透视表1", _
DefaultVersion:=xlPivotTableVersion12
Cells(1, 1).Select
End Sub
|
|