首页 > 解决方案 > 无法设置 PivotItem 类的 Visible 属性

问题描述

当我从数据透视表的“更改数据源”部分更改数据时,我收到一条错误消息。我每个月都必须这样做。调试发生在“.PivotItems(pi.Name).Visible = True”这一行。有没有办法可以向 VBA 添加代码以获取在更改数据源部分输入的新数据。当我第一次使用这个宏时,它创建了 13 个数据透视表,现在有了新数据,我需要创建 24 个数据透视表,但它只创建 13 个数据透视表,然后进行调试。假设它仍在识别先前的数据。请在下面查看我的编码。


On Error GoTo errHandler
Application.DisplayAlerts = False

Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim strPF As String
Dim strPI As String

Set ws = Sheets("Invoice PVTTBL Template")
Set pt = ws.PivotTables(1)
Set pf = pt.PageFields(1)
strPF = pf.Name

For Each pi In pf.PivotItems
  strPI = Left(pi.Name, 31)
  On Error Resume Next
    Sheets(strPI).Delete
  On Error GoTo 0
  ws.Copy After:=Sheets(Sheets.Count)
  With ActiveSheet
    .Name = strPI
    With .PivotTables(1).PivotFields(strPF)
      .PivotItems(pi.Name).Visible = True
      .CurrentPage = pi.Name
    End With
  End With

Next pi

ws.Activate

MsgBox "Invoices Created"

exitHandler:
  Application.DisplayAlerts = True
  Exit Sub
errHandler:
  MsgBox "Could not create sheets"
  Resume exitHandler

End Sub

标签: excelvbapivot-table

解决方案


推荐阅读