excel - 枢轴名称未分配,如何调试?

标签 excel excel-2010 vba

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

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

任何帮助,将不胜感激。

我的代码:

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

最佳答案

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

Set pivotSource = Report.UsedRange 

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

关于excel - 枢轴名称未分配,如何调试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16250370/

相关文章:

c - 我想在 excel vba 中复制我在 c 中的嵌套 for 循环

arrays - 在 VBA 函数中访问各个数组元素

java - 使用java在excel中的两列中写入多条记录

excel - 将命名范围从一个工作表复制到另一个工作表的宏

excel - 如何点击 IE 下载对话框,即(打开、保存、另存为...)

python - 将 Excel 工作表中的日期与某个固定日期进行比较并打印值

excel - 将 Excel 数据导入 F#

sql - 如何从 select 语句中获取值到变量中?

c# - 在 64 位 Office 插件中加载 32 位 COM dll

excel - Visual Basic for Applications(VBA)编译错误: Procedure declaration does not match description of event or procedure having the same name