在Excel 2010中,我记录了创建多个数据透视表(有些在不同工作表上)的步骤宏。但是,我正在努力让代码接受“动态”数据透视表名称。我的代码最初想要自动分配下一个数据透视表号码。例如,“PivotTable23”,“PivotTable24”等。由于我从来不知道工作簿中的下一个数字是什么,我将其更改为以下内容,当然它不起作用(我不熟悉使用vb代码):
Sub TestContinueSD()
'
' TestContinueSD Macro
'
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Stmt_Volumes!R1C1:R46154C42", Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:=PivotTables(1) _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
ActiveSheet.PivotTables(1).Name = "TOTAL"
With ActiveSheet.PivotTables("TOTAL").PivotFields("desc")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("TOTAL").AddDataField ActiveSheet.PivotTables( _
"TOTAL").PivotFields("id"), "Sum of id", xlSum
With ActiveSheet.PivotTables("TOTAL").PivotFields("Sum of id")
.Caption = "Count of id"
.Function = xlCount
End With
With ActiveSheet.PivotTables("TOTAL").PivotFields("sfreq")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("TOTAL").PivotFields("txt")
.Orientation = xlPageField
.Position = 1
End With
Rows("1:1").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Font.Bold = True
With Selection.Font
.Name = "Calibri"
.Size = 14
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ThemeColor = xlThemeColorLight1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
ActiveCell.FormulaR1C1 = "TOTAL (ALL)"
Range("A1:B1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "SUMMARY"
End Sub
答案 0 :(得分:0)
您可以通过设置CreatePivotTable
方法调用的相应参数来设置数据透视表的名称:
ActiveWorkbook.PivotCaches.Create(...). _
CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="TOTAL" _
, DefaultVersion:=xlPivotTableVersion14
有关CreatePivotTable方法的详细信息,请参阅http://msdn.microsoft.com/en-us/library/office/ff839885%28v=office.15%29.aspx。