VBA を使用して、ピボットテーブルの独自のドリルスルー アクションを作成しようとしています。アクションは、追加のアクションからピボットテーブルのコンテキスト メニューから呼び出されます。ピボットテーブル コンテキスト メニューコマンド バーの [追加のアクション]コントロールの下にボタンを配置したいと考えています。ポイントは、デフォルトで、追加のアクションにはすでに(アクションが定義されていない)項目が含まれていることです。したがって、ボタンを追加した後にこれを削除したい(アクションが定義されていません)が、何も機能しません。(アクションが定義されていません)のプロパティを変更することさえできませんキャプション、表示などのコントロール。何が原因で、回避策は何ですか? これまでのコードは次のとおりです (たとえば、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はまったく機能せず、エラー「自動化エラー」がスローされます。追加のアクション自体を有効にするなど、正常に編集できますが、 (アクションが定義されていません)コントロールを削除するか、独自のものに置き換えたいと考えています。Call .Controls("(No Actions Defined)").Deleteも機能しないことに注意してください。どうやってやるの?問題をグーグルで検索しようとしましたが、うまくいきませんでした...