首页 文章

活动枢轴表名称

提问于
浏览
0

我有一个包含多个数据透视表的工作表 . 所有数据透视表的格式都相同 .

如何在工作表中使用VBA for active pivot table将多个字段移动到值?

我尝试录制宏来将字段移动到值,但当我想将宏用于另一个数据透视表时,最终必须手动更改VBA代码中的数据透视表名称 . 我想避免必须手动更改数据透视表名称 .

您可以在屏幕截图中看到,我手动将字段列表中的“值”拖动到每个数据透视表的“VALUES”区域 .

这是一个重复的任务,我想自动使用VBA,但每个数据透视表都有唯一的名称 .

Excel Screen Capture

1 回答

  • 0

    有关如何处理数据透视表的各个部分的详细概述,请参见http://peltiertech.com/referencing-pivot-table-ranges-in-vba/ .

    这应该让你开始:

    Sub Tester()
    
        Dim pt As PivotTable
    
        Set pt = ActivePivotTable
    
        If pt Is Nothing Then
            Debug.Print "No Active pivot table"
        Else
            Debug.Print "Active = " & pt.Name
            'work on pt...
        End If
    
    End Sub
    
    'get the pivot table which contains the current selection
    Function ActivePivotTable() As PivotTable
        Dim pt As PivotTable, rv
        For Each pt In ActiveSheet.PivotTables
            Debug.Print pt.Name, pt.TableRange2.Address
            If Not Intersect(Selection, pt.TableRange2) Is Nothing Then
                Set ActivePivotTable = pt
                Exit Function
            End If
        Next pt
    End Function
    

相关问题