まずはワークブックオープン時に呼び出すようにしておきます
Private Sub Workbook_Open() Call menu_create End Sub
メニューを作るロジックを作成
Public Sub menu_create()
Dim bars As CommandBar ' 現在のEXCELメニューバー
Dim bar_control As CommandBarControl ' ユーザーメニュー追加後のメニューバー
Dim bar_botton As CommandBarButton ' ユーザーメニューに追加した項目ボタン
'メニューに「ユーザーメニュー」を追加
Set bars = CommandBars.ActiveMenuBar
Set bar_control = bars.Controls.add _
(Type:=msoControlPopup, _
Temporary:=True, _
ID:=1)
With bar_control
.BeginGroup = True
.Caption = "マクロメニュー"
.Visible = True
.Enabled = True
End With
' 「ユーザーメニュー」に、個々のサブメニューを追加していく
Set bar_botton = bar_control.Controls _
.add(Type:=msoControlButton, Temporary:=True)
bar_botton.Caption = "全シート_ヘッダフッタ設定"
bar_botton.Style = msoButtonCaption
bar_botton.OnAction = "全シート_ヘッダフッタ設定"
Set bar_botton = bar_control.Controls _
.add(Type:=msoControlButton, Temporary:=True)
bar_botton.Caption = "全シート_A1フォーカス設定"
bar_botton.Style = msoButtonCaption
bar_botton.OnAction = "全シート_A1フォーカス設定"
End Sub
ワークブッククローズ時はメニューを戻すようにしておきます
Private Sub Auto_Close() Application.ScreenUpdating = False 'デフォルトのEXCELメニューバーに戻す On Error Resume Next CommandBars.ActiveMenuBar.Reset On Error GoTo 0 End Sub