枢轴名称没有被分配,如何调试?

时间:2013-04-27 09:18:35

标签: excel excel-vba excel-2010 vba

我创建了一个宏来为名为“data”的工作表中的数据创建数据透视表。我将数据透视表的名称设置为“Pivot_Data”,但是当创建数据透视表时,名称来自数据透视表的sheet1,我期待“Pivot_Data”?< / p>

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

任何帮助将不胜感激。

我的代码:

'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"

1 个答案:

答案 0 :(得分:1)

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

Set pivotSource = Report.UsedRange 

Dim tableName As String
tableName = "Pivot_Data"
pivotSheet.Name = tableName '<~~ new line to change sheet name