楼主 水星钓鱼 |
Q:如下图所示某公司2011与2010年两年的财务清单,希望通过编写代码来创建数据透视表,录制宏后的代码如下:
- Sub 宏1()
- With ActiveWorkbook.Connections("vba+SQL").OLEDBConnection
- .BackgroundQuery = True
- .CommandText = Array( _
- "select ""2011年"" as 年度,* from [2011年$a:f] union all select ""2010年"",* from [2010年$a:f]" _
- )
- .CommandType = xlCmdTable
- .Connection = Array( _
- "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\Administrator\桌面\vba+SQL.xlsm;" _
- , _
- "Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database " _
- , _
- "Password="""";Jet 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 O" _
- , _
- "LEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Com" _
- , "plex Data=False")
- .RefreshOnFileOpen = False
- .SavePassword = False
- .SourceConnectionFile = ""
- .SourceDataFile = "C:\Documents and Settings\Administrator\桌面\vba+SQL.xlsm"
- .ServerCredentialsMethod = xlCredentialsMethodIntegrated
- .AlwaysUseConnectionFile = False
- .ServerFillColor = False
- .ServerFontStyle = False
- .ServerNumberFormat = False
- .ServerTextColor = False
- End With
- With ActiveWorkbook.Connections("vba+SQL")
- .Name = "vba+SQL"
- .Description = ""
- End With
- Sheets.Add
- Workbooks("vba+SQL.xlsm").Connections.Add "vba+SQL", "", Array( _
- "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\Administrator\桌面\vba+SQL.xlsm;" _
- , _
- "Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database " _
- , _
- "Password="""";Jet 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 O" _
- , _
- "LEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Com" _
- , "plex Data=False"), Array( _
- "select ""2011年"" as 年度,* from [2011年$a:f] union all select ""2010年"",* from [2010年$a:f]" _
- ), 3
- ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
- ActiveWorkbook.Connections("vba+SQL"), Version:=xlPivotTableVersion14). _
- CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="数据透视表2", _
- DefaultVersion:=xlPivotTableVersion14
- End Sub
现在修改宏代码为如下:- Sub xyf()
- Workbooks("vba+SQL.xlsm").Connections.Add "vba+SQL", "", Array( _
- "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\Administrator\桌面\vba+SQL.xlsm;" _
- , _
- "Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database " _
- , _
- "Password="""";Jet 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 O" _
- , _
- "LEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Com" _
- , "plex Data=False"), Array( _
- "select ""2011年"" as 年度,* from [2011年$a:f] union all select ""2010年"",* from [2010年$a:f]" _
- ), 3
- ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
- ActiveWorkbook.Connections("vba+SQL"), Version:=xlPivotTableVersion14). _
- CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="数据透视表2", _
- DefaultVersion:=xlPivotTableVersion14
- ActiveWorkbook.Connections("vba+SQL").Delete
- End Sub
却运行不了了,为什么呢? A:只要将基于PivotCache创建的Pivottable的Destination设置为空即可。 修改后的代码如下:- Sub xyf()
- Workbooks("vba+SQL.xlsm").Connections.Add "vba+SQL", "", Array( _
- "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\Administrator\桌面\vba+SQL.xlsm;" _
- , _
- "Mode=Share Deny Write;Extended Properties=""HDR=YES;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Database " _
- , _
- "Password="""";Jet 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 O" _
- , _
- "LEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False;Jet OLEDB:Support Com" _
- , "plex Data=False"), Array( _
- "select ""2011年"" as 年度,* from [2011年$a:f] union all select ""2010年"",* from [2010年$a:f]" _
- ), 3
- ActiveWorkbook.PivotCaches.Create(SourceType:=xlExternal, SourceData:= _
- ActiveWorkbook.Connections("vba+SQL"), Version:=xlPivotTableVersion14). _
- CreatePivotTable TableDestination:="", TableName:="数据透视表2", _
- DefaultVersion:=xlPivotTableVersion14
- ActiveWorkbook.Connections("vba+SQL").Delete
- End Sub
vba+SQL.rar
|