【问题标题】:Writing VBA to filter through OLAP Pivot Table Fields编写 VBA 以过滤 OLAP 数据透视表字段
【发布时间】:2021-02-25 14:27:08
【问题描述】:

我是 VBA 新手。我有一个带有由 OLAP Cube 创建的数据透视表的 excel 文件。 我尝试创建一个变量来过滤数据透视表中的特定列。

我使用了录制宏,这就是我所拥有的:

Sub Macro1()

    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "[Item].[ItemByProducer].[ProducerName]").VisibleItemsList = Array( _
        "[Item].[ItemByProducer].[ProducerName].&[the name of the producer]")

End Sub

该宏适用于特定的生产者,但我想创建一个字符串类型的变量,我也可以在其中过滤其他生产者。

我怎样才能做到这一点?

【问题讨论】:

  • 您可以将字符串与&连接起来:"[Item].[ItemByProducer].[ProducerName].&[" & yourvariable & "]"
  • 实际上这似乎可以解决问题 Sub Macro1() ActiveSheet.PivotTables("PivotTable1").PivotFields( _ "[Item].[ItemByProducer].[ProducerName]").VisibleItemsList = Array( _ "[Item].[ItemByProducer].[ProducerName].&["& stringvariable &"]") End Sub

标签: excel vba pivot-table olap-cube


【解决方案1】:

将变量放入数组中 - producers = Array("ProducerA", "ProducerB", "ProducerC")

然后用下面的代码循环它们。只需确保将 "NameOfWorksheet" 的名称更改为您正在使用的工作表即可。 ActiveSheet 不是一个好主意 - How to avoid using Select in Excel VBA

Sub TestMe()

    Dim producers As Variant
    producers = Array("ProducerA", "ProducerB", "ProducerC")
    Dim producer As Variant
    
    For Each producer In producers
        ThisWorkbook.Worksheets("NameOfWorksheet").PivotTables("PivotTable1").PivotFields( _
            "[Item].[ItemByProducer].[ProducerName]").VisibleItemsList = Array( _
                "[Item].[ItemByProducer].[ProducerName].&[" & producer & "]")
    Next    

End Sub

【讨论】:

    猜你喜欢
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 2018-09-12
    • 2016-12-24
    相关资源
    最近更新 更多