2

我创建了一个宏来为名为"data"的工作表中存在的数据创建数据透视。我将枢轴的名称输入为“Pivot_Data”,但是当创建枢轴时,该名称作为数据透视表的 sheet1 出现,我期待“Pivot_Data”

也可以帮助调试数据透视缓存吗?我的意思是除了宏中的任何监视之外,数据如何缓存在数据透视缓存中以查看数据透视缓存中的内容和内容

任何帮助,将不胜感激。

我的代码:

'To set up my workbook & worksheet variables.
Dim bReport As Workbook, Report As Worksheet, pivotSheet As Worksheet     
Set bReport = Excel.ActiveWorkbook
Set Report = bReport.Worksheets("data") 'Create the worksheet to place the SQL data
Set pivotSheet = bReport.Worksheets.Add 'Create the worksheet to place the Pivot Table

Dim pivotSource As Range 'To set up the variable representing your pivot data.
Set pivotSource = Report.UsedRange 'selecting entire data in the sheet

Dim tableName As String
tableName = "Pivot_Data"  'name of pivot report i wanted to create from data in sheet Prod

bReport.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=pivotSource).CreatePivotTable _
    TableDestination:=pivotSheet.Cells(1, 1), tableName:=tableName

Set pt = pivotSheet.PivotTables(tableName)
pivotSheet.PivotTableWizard TableDestination:=pivotSheet.Cells(1, 1)
Set pOne= pt.PivotFields("Number")
Set pTwo = pt.PivotFields("Premium")
Set pthree = pt.PivotFields("TransactoinID")
Set pFour = pt.PivotFields("money")

pOne.Orientation = xlRowField 'This assigns the orientation of a given field to xlRowField.
pTwo.Orientation = xlRowField
pTwo.Subtotals(1) = False 'This denotes there will be no subtotal for this field.
pThree.Orientation = xlRowField
pThree.Subtotals(1) = False
pFour.Orientation = xlDataField
pFour.NumberFormat = "$#,##0.00"
4

1 回答 1

1

要命名工作表,您需要添加另一行代码来重命名它:

Set pivotSource = Report.UsedRange 

Dim tableName As String
tableName = "Pivot_Data"
pivotSheet.Name = tableName '<~~ new line to change sheet name
于 2013-04-28T04:57:34.593 回答