excel自定义问题

来源:百度知道 编辑:UC知道 时间:2024/06/29 00:41:31
我做了一个宏,想自定义一个菜单,把宏弄上去
但我发现一旦自定义菜单,所有的excel都会有这个菜单
但是我只想这个excel文件有这个菜单,其他的都不要有,怎么做?

要在这个文件用VBA代码写一个菜单。

在ThisWorkbook中写入:

Private Sub Workbook_Open()
添加菜单栏名称
End Sub

Sub 添加菜单栏名称()
On Error Resume Next
Application.CommandBars("我的主菜单").Delete
Dim 主菜单栏 As CommandBar
Set 主菜单栏 = Application.CommandBars.Add
With 主菜单栏
.Visible = True
.Name = "我的主菜单"
End With

Dim MC As CommandBarButton
Set MC = Application.CommandBars("我的主菜单").Controls.Add
With MC
.FaceId = 2950
.BeginGroup = True
.OnAction = "Macro1"
End With

End Sub

注:
把 "Macro1" 修改为你的宏名即可。

可以在Workbook_Activate事件中加入添加菜单的代码,在Workbook_Deactivate事件中添加删除菜单的代码,具体实例请参考:
http://www.excelba.com/showdown.asp?soft_id=15
中的相关代码。