使用 VBA 宏 (Excel) 将切片器设置为变量的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18379407/
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-11 22:54:11  来源:igfitidea点击:

Setting a slicer to the value of a variable using VBA macro (Excel)

excelexcel-vbavba

提问by Predator

This is a very trivial question for the experts out here :)

对于这里的专家来说,这是一个非常微不足道的问题:)

I'm trying to set a slicer to the value of a variable using a macro. I've declared a String variable.

我正在尝试使用宏将切片器设置为变量的值。我已经声明了一个 String 变量。

Dim ProductName As String
'+Some code here to assign a value to ProductName (value of a cell)
ActiveWorkbook.SlicerCaches("Slicer_Name1").VisibleSlicerItemsList = Array( _
            "[Team Project].[Project Node Name].&[ProductName]")

That obviously does not work and I didn't expect it to but I can't figure out how I can have it treat ProductName as a variable and not a literal.

这显然不起作用,我没想到它会起作用,但我不知道如何让它将 ProductName 视为变量而不是文字。

The rest of the code is fine as I can successfully set the slicer by specifying a product name without using a variable.

其余代码很好,因为我可以通过指定产品名称而不使用变量来成功设置切片器。

ActiveWorkbook.SlicerCaches("Slicer_Name1").VisibleSlicerItemsList = Array( _
            "[Team Project].[Project Node Name].&[Bedsheets]")

回答by

Stick the variable out of the string

将变量从字符串中取出

Dim ProductName As String
ActiveWorkbook.SlicerCaches("Slicer_Name1").VisibleSlicerItemsList = Array( _
            "[Team Project].[Project Node Name].&[" & ProductName & "]" )