|
本帖最后由 OKJSJSF 于 2021-4-19 07:40 编辑
在一个工作簿中联合多个工作表再创建透视表的方法适合数据记录较少的情况,如果记录多到超过表格上限就不行(比如联合每天的数据记录,一年366天,比如数据范围扩大至全系统、从村、乡镇扩大至县、市或省)。所以决定学习采用多表直接创建透视表。但为多表写SQL命令也较麻烦,于是决定改为小程序,
按照《excel2007实战技巧精粹》技巧164“多表动态汇总”的办法录了一个宏(空白的透视表,字段由用户自行拖放,以不变应万变),发现比没有采用“连接‘的单表生成透视表的程序多了太多代码,而且代码顺序与操作顺序并不同,想改成单击按钮执行事件过程的通用宏,不知哪些语句是多余的,怎么改动?数据连接的知识一穷二白,特求助。下面是录制的宏,包含一个工作簿中的四个表,黄色底纹是变量。
Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
'
With ActiveWorkbook.Connections("贷款明细2").OLEDBConnection
.BackgroundQuery = True
.CommandText = Array( _
"select ""贷"" , * from [贷$] union all select ""款"" , * from [款$] union all select ""明"" , * from [明$] union all select ""细""" _
, " , * from [细$]")
.CommandType = xlCmdTable
.Connection = Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Users\Administrator\Desktop\贷款明细2.xlsm;Mode=Share Den" _
, _
"y Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Je" _
, _
"t OLEDB:Engine 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 Cop" _
, _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=Fals" _
, "e")
.RefreshOnFileOpen = True
.SavePassword = False
.SourceConnectionFile = ""
.SourceDataFile = "C:\Users\Administrator\Desktop\贷款明细2.xlsm"
.ServerCredentialsMethod = xlCredentialsMethodIntegrated
.AlwaysUseConnectionFile = False
.ServerFillColor = False
.ServerFontStyle = False
.ServerNumberFormat = False
.ServerTextColor = False
End With
With ActiveWorkbook.Connections("贷款明细2")
.Name = "贷款明细2"
.Description = ""
End With
Workbooks("贷款明细2.xlsm").Connections.Add "贷款明细2", "", Array( _
"OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Users\Administrator\Desktop\贷款明细2.xlsm;Mode=Share Den" _
, _
"y Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database Password="""";Je" _
, _
"t OLEDB:Engine 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 Cop" _
, _
"y Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Complex Data=Fals" _
, "e"), Array( _
"select ""贷"" , * from [贷$] union all select ""款"" , * from [款$] union all select ""明"" , * from [明$] union all select ""细""" _
, " , * from [细$]"), 3
ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
ActiveWorkbook.Connections("贷款明细2"), Version:=xlPivotTableVersion12). _
CreatePivotTable TableDestination:="多表动态汇总!R1C1", TableName:="数据透视表1", _
DefaultVersion:=xlPivotTableVersion12
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
ActiveSheet.PivotTables("数据透视表1").ShowDrillIndicators = False
ActiveSheet.PivotTables("数据透视表1").HasAutoFormat = False
ActiveSheet.PivotTables("数据透视表1").RowAxisLayout xlTabularRow
End Sub
|
|