简体   繁体   English

Excel VBA导出数据透视表

[英]Excel VBA export pivot table

the workbook: In my workbook the user completes a table with data. 工作簿:在我的工作簿中,用户使用数据完成一个表。 When clicking a command button the VBA code (see below) updates a PivotTable on another worksheet and then splits this table into different worksheets based on the different values filled in the pivotfield " Monstertype " (each pivotitem in that pivotfield becomes a new sheet with that table). 单击命令按钮时,VBA代码(请参见下文)将更新另一个工作表上的数据透视表,然后根据在“ Monstertype” 枢轴字段中填充的不同值将此表拆分为不同的工作表(该枢轴字段中的每个枢轴项目都将成为一个新的工作表表)。 These sheets are later transferred to a word document. 这些工作表随后被转移到Word文档中。

The problem: On the first trial the first pivotitem to encounter was called "Hygiene control", it then worked fine. 问题:在第一次试用中,遇到的第一个枢轴项目称为“卫生控制”,然后运行良好。 On later trials it keeps looking for the pivotitem "Hygiene control" even if its not there anymore, with as a result I receive the error that it falls outside the subscript. 在以后的试验中,即使它不再存在,它仍在寻找枢纽项目“卫生控制”,结果我收到了错误消息,该错误不在下标之内。

Sub exportPivot()
Application.ScreenUpdating = False

Worksheets("Blad9").PivotTables("Draaitabel17").RefreshTable

Dim wbbron As Workbook
Dim wbdoel As Workbook

Dim shtbron As Worksheet
Dim shtdoel As Worksheet

Dim pvt As PivotTable
Dim pi As PivotItem

Set wbbron = ActiveWorkbook
Set shtbron = wbbron.Sheets("Blad9")
Set pvt = shtbron.PivotTables("Draaitabel17")

pvt.ShowPages Pagefield:="Monstertype"

Set wbdoel = Workbooks.Add

For Each pi In pvt.PageFields("Monstertype").PivotItems
    wbbron.Sheets(pi.Value).Move after:=wbdoel.Sheets(wbdoel.Sheets.Count) 
Next pi

Application.DisplayAlerts = False
wbdoel.Sheets(1).Delete
Application.DisplayAlerts = True

VulOfferte wbdoel

End Sub

The error occurs on 错误发生在

wbbron.Sheets(pi.Value).Move after:=wbdoel.Sheets(wbdoel.Sheets.Count)

the cry for help: It drives me crazy, please help. 寻求帮助的呼声:这让我发疯,请帮助。

The code below will loop through PivotItems in PivotField " Monstertype ", and will copy a Worksheet to the added Workbook for each Item with its' CurrentPage. 下面的代码将遍历PivotFieldMonstertype ”中的PivotItems ,并将每个工作表的CurrentPage复制到工作表中。

Sub exportPivot()

Dim wbbron          As Workbook
Dim wbdoel          As Workbook

Dim shtbron         As Worksheet
Dim shtdoel         As Worksheet
Dim SrcData         As Range

Dim pvt             As PivotTable
Dim PvtCache        As PivotCache
Dim PvtFld          As PivotField
Dim pi              As PivotItem

Application.ScreenUpdating = False

Set wbbron = ActiveWorkbook
Set shtbron = wbbron.Sheets("Blad9")

' added a Range to dynamic read new rows that were added to the source data of the Pivot Table
' modify "Sheet1" to your Sheet's name (where the Pivot Table data is located)
Set SrcData = Sheets("Sheet1").Range("A1:C" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)

' set Pivot Cache to updated Pivot Table Source Data
Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)

Set pvt = shtbron.PivotTables("Draaitabel17")
pvt.ChangePivotCache PvtCache ' set updated Cache to Pivot Table
pvt.RefreshTable

Set PvtFld = pvt.PivotFields("Monstertype")

Set wbdoel = Workbooks.Add

' loop through all Pivot Items in Pivot Field "Monstertype" , and copy a new Sheet to added workbook for every item
For Each pi In PvtFld.PivotItems
    PvtFld.CurrentPage = pi.Name

    shtbron.Copy after:=wbdoel.Sheets(wbdoel.Sheets.Count)
    With ActiveSheet
        .Name = pi.Name
        pi.Visible = True
        'PvtFld.CurrentPage = pi.Name
    End With
   ' wbbron.Sheets(pi.Name).Copy after:=wbdoel.Sheets(wbdoel.Sheets.Count)
Next pi

Application.DisplayAlerts = False
wbdoel.Sheets(1).Delete

' you can remove also sheets(2) and (3) if you want to clean-up
'wbdoel.Sheets(2).Delete
'wbdoel.Sheets(3).Delete

Application.DisplayAlerts = True

' don't know this command (NOT IN ENGLISH)
'VulOfferte wbdoel

End Sub

This looks like your pivot table retains items deleted from the data source section. 看起来您的数据透视表保留了从数据源部分删除的项目。 This is not a problem of your VBA code, which looks fine to me. 这不是您的VBA代码的问题,对我来说看起来不错。 You can change this by right clicking any cell of your pivot table, select "Pivot Table Options". 您可以通过右键单击数据透视表的任何单元格,然后选择“数据透视表选项”来更改此设置。 Then click on the "data" tab and change "Retain items deleted from the data source section" to "None". 然后单击“数据”选项卡,并将“保留从数据源部分删除的项目”更改为“无”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM