0

Excel 数据透视表在 VBA 中编码是一件令人头疼的事情!

我想要一个循环遍历每个数据透视表的 VBA 代码,然后是数据透视字段,并且对于每个“(空白)”数据透视项目,将其移动到位置 1 或最后一个位置。

谢谢你的帮助!

For Each pt In ws.PivotTables

pt.RefreshTable
pt.PivotCache.MissingItemsLimit = xlMissingItemsNone

For Each pf In pt.PivotFields

For Each pi In pf.PivotItems

If pi.Caption = "(blank)" Then

pi.position = 1 ' <-- Error 2024, not available? 

If pi.Visible = True Then
pi.Visible = False
End if 

Exit For
End If

Next pi 
Next pf
Next pt 
4

1 回答 1

2

在我有限的测试中,这是没有错误的并且有效:

Sub HideAndMoveTheBlanks()

Dim pt As Excel.PivotTable
Dim pf As Excel.PivotField
Dim pi As Excel.PivotItem

For Each pt In ws.PivotTables
    pt.RefreshTable
    pt.PivotCache.MissingItemsLimit = xlMissingItemsNone
    For Each pf In pt.PivotFields
        On Error Resume Next
        Set pi = pf.PivotItems("(blank)")
        On Error GoTo 0
        If Not pi Is Nothing Then
            pi.Position = 1
            pi.Visible = False
            Set pi = Nothing
            Exit For
        End If
    Next pf
Next pt
End Sub
于 2012-05-24T05:06:41.713 回答