我在excel里面用VBA绘制了一个窗体UserForm1,需要在菜单栏添加一个按钮,单击按钮就弹出UserForm1。

2024-11-11 13:33:15
推荐回答(3个)
回答(1):

你是要在Excel的菜单手袭缓上点击吗?
那样你的添加一个菜单:
Private Sub Workbook_Open()
Dim TargetBar As CommandBar
Dim NewMenu As Object
Dim NewItem As Object
Dim NewMenuTemp As Object

Set TargetBar = Application.CommandBars("Worksheet Menu Bar")
TargetBar.Visible = True

For Each NewMenuTemp In TargetBar.Controls
If NewMenuTemp.Caption = "Function" Then
Exit Sub
End If
Next
Set NewMenu = TargetBar.Controls.Add(Type:=msoControlPopup, ID:=1, Temporary:=True)
NewMenu.Caption = "Function"

Set NewItem = NewMenu.Controls.Add(Type:=msoControlButton, ID:=1, Temporary:=True)
NewItem.Caption = "毕模Function 1"
NewItem.OnAction = "模块1.function1"

Exit Sub
End Sub

以上代码禅告放到thisworkbook里,然后在模块1里添加function1
sub function1()
UserForm1.show
end sub
这样excel启动后会在excel的菜单上新增个菜单Function,点击里边的Function1就可以了。

回答(2):

UserForm1.show

回答(3):

xieyb0417老师的代码就非常好,完全达到你的要求