vba 如何在excel vba中单击切片器中的值时运行宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27998731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:17:50  来源:igfitidea点击:

how to run a macro while clicking on a value in slicer in excel vba

excelexcel-vbavba

提问by user3305327

I have a Slicer in my excel with the following 2 values

我的 excel 中有一个切片器,其中包含以下 2 个值

Add Customer Add Vendor

添加客户 添加供应商

I want to open 2 different forms when click on those value. Can anyone please help me with this.

单击这些值时,我想打开 2 个不同的表单。任何人都可以帮我解决这个问题。

回答by Paresh J

Yes, you can call different forms on slicer clicks, check the below code. Hope it suits your requirement and if not you can use to change as per your requirement.

是的,您可以在切片器点击时调用不同的表单,请检查以下代码。希望它适合您的要求,如果不是,您可以根据您的要求进行更改。

On Writing your code in Worksheet_PivotTableUpdateevent, on each slicer click this event will get called.

Worksheet_PivotTableUpdate事件中编写代码时,在每个切片器上单击此事件将被调用。

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)

If ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("East").Selected = True Then
UserForm1.Show
ElseIf ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("West").Selected = True Then
UserForm2.Show
End If

End Sub

Where, Slicer_Region = "Name to use in formulas"

其中, Slicer_Region = "要在公式中使用的名称"

You can get this name by right click on slicer and click slicer settings. and find the name to use in formula.

您可以通过右键单击切片器并单击切片器设置来获取此名称。并找到要在公式中使用的名称。

East or West = "Names of slicers"

东或西 =“切片器的名称”

For which you want to call different forms.

您要为其调用不同的形式。