【问题标题】:Excel VBA: Can't set pivotitem "(blank)" position property in Pivot TableExcel VBA:无法在数据透视表中设置数据透视表“(空白)”位置属性
【发布时间】:2012-05-30 15:24:12
【问题描述】:

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 

【问题讨论】:

    标签: arrays excel vba pivot-table


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-09
      • 1970-01-01
      • 2016-08-30
      相关资源
      最近更新 更多