我尝试使用VBA在OLAP Slicers上选择多个项目。
这样做的文字命令的例子是:
ThisWorkBook.SlicerCaches("Some_Slicer_Cache").VisibleSlicerItemsList = Array( _
"[D_MUMAP].[MU_ID].&[item1]", "[D_MUMAP].[MU_ID].&[item2]")
我拉"物品"从逗号分隔的列表(在另一个单元格中)
item1, item2
将它们处理成一个数组,然后将整个字符串输出到另一个单元格(我猜不是真的有必要,但我喜欢视觉效果)。
[D_MUMAP].[MU_ID].&[item1], [D_MUMAP].[MU_ID].&[item2]
当只有一个项目时,此数组进程有效,显然是因为已经隐含了唯一的引号集。如果我尝试使用多个运行它,我会收到以下错误:
Run-time error '1004':
The query did not run or the Data Model could not be accessed.
Query (1,25) The syntax for ',' is incorrect.
([D_MUMAP].[MU_ID].&[item1], [D_MUMAP].[MU_ID].&[item2]).
我应该尝试解决此问题的后续步骤?
答案 0 :(得分:1)
我相信以下内容适用于哪种情况下您根本不需要处理双引号:
Sub Foo()
Dim list As String
'the input list of items
list = "item1,item2,item3"
'split this into an array
Dim listArray
listArray = Split(list, ",")
'for each element, prepend [D_MUMAP].[MU_ID].&[ and append ]
Dim i As Integer
For i = 0 To UBound(listArray)
listArray(i) = "[D_MUMAP].[MU_ID].&[" & listArray(i) & "]"
Next i
'dump array for interest's sake
Debug.Print Join(listArray, ", ")
'update slicer
ThisWorkbook.SlicerCaches("Some_Slicer_Cache").VisibleSlicerItemsList = listArray
End Sub