特定的命令栏不会在closures菜单中closures

我写了一个脚本,在上下文菜单中显示特定的脚本。 编程之后,上下文菜单显示我想要启动的脚本,脚本运行良好。

'SCRIPT POUR MENU CONTEXTUEL Sub AddToCellMenu() Dim ContextMenu As CommandBar Dim MySubMenu As CommandBarControl ' Delete the controls first to avoid duplicates. Call DeleteFromCellMenu ' Set ContextMenu to the Cell context menu. Set ContextMenu = Application.CommandBars("Cell") ' Add one custom button to the Cell context menu "BL OK". With ContextMenu.Controls.Add(Type:=msoControlButton, before:=20) .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_BonLivraisonOK" .FaceId = 71 .Caption = "Bon Livraison OK" .Tag = "My_Cell_Control_Tag" End With ' Add one custom button to the Cell context menu "LIVRAISON OK". With ContextMenu.Controls.Add(Type:=msoControlButton, before:=21) .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_LivraisonOK" .FaceId = 72 .Caption = "Expédition OK" .Tag = "My_Cell_Control_Tag" End With ' Add one custom button to the Cell context menu "Livraison avec RELIQUAT". With ContextMenu.Controls.Add(Type:=msoControlButton, before:=22) .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_LivraisonReliquat" .FaceId = 73 .Caption = "Expédition avec RELIQUAT" .Tag = "My_Cell_Control_Tag" End With ' Add one custom button to the Cell context menu "RAZ". With ContextMenu.Controls.Add(Type:=msoControlButton, before:=23) .OnAction = "'" & ThisWorkbook.Name & "'!" & "Bouton_CellBlank" .FaceId = 70 .Caption = "RAZ" .Tag = "My_Cell_Control_Tag" End With ' Add a separator to the Cell context menu. ContextMenu.Controls(20).BeginGroup = True End Sub 

问题是closures文件后,contextmenu仍然在其他excel文件中运行。 当我在其他Excel文件上工作时,我需要您的帮助来closures上下文菜单中的function。

 Sub DeleteFromCellMenu() Dim ContextMenu As CommandBar Dim ctrl As CommandBarControl ' Set ContextMenu to the Cell context menu. Set ContextMenu = Application.CommandBars("Cell") ' Delete the custom controls with the Tag : My_Cell_Control_Tag. For Each ctrl In ContextMenu.Controls If ctrl.Tag = "My_Cell_Control_Tag" Then ctrl.Delete End If Next ctrl ' Delete the custom built-in Save button. On Error Resume Next ContextMenu.FindControl(ID:=3).Delete On Error GoTo 0 End Sub 

我find了解决scheme。 我必须在工作簿中声明CellMenu的开启/closures。 请参考以下代码:

  Private Sub Workbook_Activate() Call AddToCellMenu End Sub Private Sub Workbook_Deactivate() Call DeleteFromCellMenu End Subenter code here