VBA 数据透视表向导对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12076623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:26:13  来源:igfitidea点击:

VBA PivotTableWizard Object

vbaexcel-vbaexcel

提问by AME

I created a subroutine based off of the example VBA code on Microsoft's tutorial page: LINK

我根据 Microsoft 教程页面上的示例 VBA 代码创建了一个子例程:LINK

Sub WIP20Pivot()
'
' Pivot Macro

'Creates a PivotTable report from the table on Sheet1
'by using the PivotTableWizard method with the PivotFields
'method to specify the fields in the PivotTable.
 Dim objTable As PivotTable, objField As PivotField

' Select the sheet and first cell of the table that contains the data.
 ActiveWorkbook.Sheets("DATA").Select
 Range("A1").Select

' Create the PivotTable object based on the Employee data on Sheet1.
 Set objTable = Sheet1.PivotTableWizard 'ERROR OCCURS HERE
 ActiveSheet.Name = "PivotSheet" 

'There is more VBA code below to select fields for the pivot table, etc..

End Sub

And I receive the following error, when I run this code:

当我运行此代码时,我收到以下错误:

Run-time error '424': "Object required".

运行时错误“424”:“需要对象”。

What's strange is that the subroutine works perfectly fine in Microsoft's downloadable example macro on their tutorial page.

奇怪的是,该子程序在其教程页面上的 Microsoft 可下载示例宏中运行得非常好。

Thanks in advance for you help!

在此先感谢您的帮助!

Thanks, AME

谢谢,阿梅

回答by Ashok

Set objTable = Sheet1.PivotTableWizard 'ERROR OCCURS HERE

设置 objTable = Sheet1.PivotTableWizard '错误发生在这里

you have specified Shee1 however the real sheet name would have been different. you would have deleted some sheets and may be you named the sheet as sheet1. Please check in project window in vba editor your actual sheet name will be displayed like "Sheet9(Sheet1)". In this scenario you need to mention as

您已指定 Shee1,但实际工作表名称会有所不同。您会删除一些工作表,并且可能将工作表命名为 sheet1。请在 vba 编辑器中检查项目窗口,您的实际工作表名称将显示为“Sheet9(Sheet1)”。在这种情况下,您需要提及为

"Set objTable = Sheet9.PivotTableWizard"

“设置 objTable = Sheet9.PivotTableWizard”

thats it solved!!! :)

就这样解决了!!!:)

(Note your codes should be in Module)

(注意你的代码应该在模块中)

回答by topseer

I have the same problem. After I did some research, I found this solution. Hope it can also help you.

我也有同样的问题。在我做了一些研究之后,我找到了这个解决方案。希望它也能帮到你。

 Set objTable = Worksheets("PivotTable").PivotTableWizard(SourceType:=xlDatabase, SourceData:=Range("PivotData!A1:C3918"), tabledestination:=Range("PivotTable!A1"))

回答by Francis Ho

Your Sub WIP20Pivot() => subroutinemust be in a module,

Sub WIP20Pivot() => subroutine必须在一个模块中,

cannot be embedded in a worksheet.

不能嵌入到工作表中。

It happen to me too.

它也发生在我身上。

回答by ingalcala

I guess this helps to name the Pivot sheet?

我想这有助于命名数据​​透视表?