简体   繁体   English

VBA选择切片器项目Excel

[英]vba select slicer item excel

I've recently discovered VBA code to filter slicers based off of variable names. 我最近发现了VBA代码,可根据变量名称过滤切片器。 It is a great tool for filtering what you want to see 这是过滤您想要看到的东西的好工具

The next step in my code is to potentially REMOVE visible data from my pivot table/chart (automatically). 代码的下一步是潜在地(自动)从数据透视表/图表中删除可见数据。

Lets say I already have a variable "Remove_ITEM" that needs to be removed from the data shown. 可以说我已经有一个变量“ Remove_ITEM”,该变量需要从显示的数据中删除。 Remove_item is inside slicer ("slicer_Order"). Remove_item位于切片器中(“ slicer_Order”)。

The data is also inside a data model. 数据也位于数据模型中。

The code below is to ONLY show REmove_Item: 下面的代码仅显示REmove_Item:

ActiveWorkbook.SlicerCaches("Slicer_Order").'VisibleSlicerItemsList = ("[Actuals_Table].[Order].&["& Remove_item &"]")

Now i want to do the opposite 现在我想做相反的事情

I hope I understood what you are trying to achieve in your post. 我希望我能理解您在帖子中要实现的目标。

Try the code below and let me know if it works as you intended: 试试下面的代码,让我知道它是否按预期工作:

Option Explicit

Sub SlicersTst()

Dim WB                      As Workbook
Dim OrderSlcrCache          As SlicerCache
Dim OrderSlcItem            As SlicerItem
Dim RemoveItem              As Variant

Set WB = ThisWorkbook

Set OrderSlcrCache = WB.SlicerCaches("Slicer_Order") '<-- set Slicer Cache to "Order" slicer
OrderSlcrCache.ClearManualFilter '<-- clear manual filters

RemoveItem = "c" '<-- set value for test

' loop through all slicer items in slicer "Order"
For Each OrderSlcItem In OrderSlcrCache.SlicerItems
    If OrderSlcItem.Name = RemoveItem Then OrderSlcItem.Selected = False
Next OrderSlcItem

End Sub

Please keep in mind this is specifically for data model usage with a connection. 请记住,这是专门用于连接的数据模型。

Sub Variables()
Part_Number = Worksheets("Solumina_Data_Page").Cells(Row, "B").Value
End Sub


Sub Sort_Part_Number()
Application.ScreenUpdating = False
Page1 = ActiveSheet.Name
Call Variables


Sheets("Dashboard").Activate

    ActiveWorkbook.SlicerCaches("Slicer_Material").VisibleSlicerItemsList = "[Part List].[Material].&[" & Part_Number & "]"
'    "[Part List].[Material].&[77C726210G1]" <<What we want to see

Sheets(Page1).Activate
Application.ScreenUpdating = True
End Sub

Heres a similar example using the array 这是使用数组的类似示例

Sub Use_ARRAY()


Dim ARR() As String
ReDim ARR(1 To 2)
Call Array_Actuals_Data 'get data
Call Array_Outliers_removed 'sort data

ARR(1) = "[Actuals_Table].[Order].&[000010840921]"
ARR(2) = "[Actuals_Table].[Order].&[000010949159]"

ActiveWorkbook.SlicerCaches("Slicer_order").VisibleSlicerItemsList = ARR()

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM