切片器修改属性

时间:2019-07-09 17:01:42

标签: excel vba

尝试使用VBA生成数据透视表和关联的切片器。我能够很好地生成表和切片器,但是在尝试修改切片器属性时出现错误5。错误在“ With”行中。我目前有什么毛病?

试图将名称值更改为实际的Slicer_ShipQuantity。并在网上四处张望,但我看不到任何解决方案。我确实找到了一个来自该网站的https://www.microsoft.com/en-us/microsoft-365/blog/2011/01/27/control-slicers-by-using-vba/,但是,当我使用它/对它进行类型复制时,它似乎不起作用。

'This works to create slicer
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("MasterPivot"), "ShipQuantity") _
        .Slicers.Add ActiveSheet, , "ShipQuantity", "ShipQuantity", _
        RowLocation, ColumnLocation, width, Height

'this does not work to modify slicer settings. And I have no idea why.
With ActiveWorkbook.SlicerCaches("ShipQuantity").Slicers("ShipQuantity")
        .NumberOfColumns = 3
        .RowHeight = 13
        .ColumnWidth = 70
End With

期望得到代码来修改显示的切片器,而不会出现任何错误。拜托,谢谢!

1 个答案:

答案 0 :(得分:2)

Slicers.Add返回对添加的切片器的引用,因此您可以直接使用该引用

https://docs.microsoft.com/en-us/office/vba/api/excel.slicers.add

Dim slcr

Set slcr = ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("MasterPivot"), "ShipQuantity") _
        .Slicers.Add(ActiveSheet, , "ShipQuantity", "ShipQuantity", _
        RowLocation, ColumnLocation, width, Height)


With slcr
        .NumberOfColumns = 3
        .RowHeight = 13
        .ColumnWidth = 70
End With