我正在尝试通过 VBA 为数据透视表进行自己的钻取操作。该操作将从Additional Actions的 PivotTable 的上下文菜单中调用。我想将我的按钮放在数据透视表上下文菜单命令栏的附加操作控件下。关键是默认情况下,附加操作已经包含(未定义操作)项。所以,我想在添加按钮后删除它(未定义操作),但没有任何效果。我什至无法更改(未定义操作)的任何属性控制,如 Caption、Visible 等。可能是什么原因,解决方法是什么?到目前为止,这是我的代码(例如,您可以将其放在 Workbook_SheetBeforeRightClick 下,然后使用该工作簿中的任何数据透视表进行测试):
Dim PCell As PivotCell
Dim PComBar As CommandBar
Dim PControl As CommandBarControl
Dim DControl As CommandBarControl
Dim BControl As CommandBarControl
Dim IsFromPivotTable As Boolean
IsFromPivotTable = False
On Error GoTo NotFromPivot
Set PCell = Target.PivotCell
IsFromPivotTable = True
NotFromPivot:
On Error GoTo 0
If IsFromPivotTable Then
Set PComBar = Application.CommandBars("PivotTable Context Menu")
Set PControl = PComBar.Controls("Additional Actions")
On Error Resume Next
With PControl
Call .Controls("My Drillthrough Action").Delete
.Enabled = True
End With
On Error GoTo 0
Set DControl = PControl.Controls.Add(Type:=msoControlButton, Temporary:=True, Before:=1)
With DControl
.Style = msoButtonIconAndCaption
.Caption = "My Drillthrough Action"
.FaceId = 786
End With
On Error Resume Next
Set BControl = PControl.Controls("(No Actions Defined)")
With BControl 'This does not work and throws error if do not suppress with On Error
.Enabled = True
.Visible = False
.Caption = "Hello there"
End With
On Error GoTo 0
End If
因此,最后一部分With BControl ... End With根本不起作用,并引发错误“自动化错误”。我可以成功地编辑Additional Actions本身,比如启用它,但我想摆脱(未定义操作)控件,或者用我自己的替换它。请注意,Call .Controls("(No Actions Defined)").Delete也不起作用。我怎样才能做到这一点?我试图用谷歌搜索这个问题,但没有运气......