|
本帖最后由 010203400 于 2015-6-18 08:12 编辑
各位大大, 對于添加工具欄按鈕方面, 我一直是弱項, 七拼八湊的寫了一段宏,
但總覺的如下還可以再簡化, 應當可以寫成1行或2行的, 請大家幫忙指導一下.
Set SubMenu_Command = SubMenu.Controls.Add(Type:=msoControlButton)
SubMenu_Command.OnAction = "Button02"
SubMenu_Command.Caption = "按鈕2"
SubMenu_Command.FaceId = 59
- Sub CommandButton1()
- On Error Resume Next
- Application.CommandBars("GreenDongForWO").Delete
- Set GreenDongForWO = Application.CommandBars.Add
- GreenDongForWO.Name = "GreenDongForWO"
- GreenDongForWO.Position = msoBarTop
- GreenDongForWO.MenuBar = True
- GreenDongForWO.Visible = True
- Set SubMenu = GreenDongForWO.Controls.Add(Type:=msoControlPopup)
- SubMenu.Caption = "按鈕"
- Set SubMenu_Command = SubMenu.Controls.Add(Type:=msoControlButton)
- SubMenu_Command.OnAction = "Button01"
- SubMenu_Command.Caption = "按鈕1"
- SubMenu_Command.FaceId = 59
- Set SubMenu_Command = SubMenu.Controls.Add(Type:=msoControlButton)
- SubMenu_Command.OnAction = "Button02"
- SubMenu_Command.Caption = "按鈕2"
- SubMenu_Command.FaceId = 59
- ThisWorkbook.Close False
- End Sub
复制代码
Sub test1()
Call test2
'1)创建并设置命令栏
Set GreenDongForWO = Application.CommandBars.Add
GreenDongForWO.Name = "GreenDongForWO"
GreenDongForWO.Position = msoBarTop '在顶部
GreenDongForWO.Visible = True
'2)在该命令栏中,创建弹出框
Set SubMenu = GreenDongForWO.Controls.Add(Type:=msoControlPopup)
SubMenu.Caption = "按鈕"
'3)在该弹出框中,创建命令按钮
For i = 1 To 2
Set SubMenu_Command = SubMenu.Controls.Add(Type:=msoControlButton)
SubMenu_Command.OnAction = "Button0" & i
SubMenu_Command.Caption = "按鈕" & i
SubMenu_Command.FaceId = 59
Next i
End Sub
Sub test2()
On Error Resume Next
Application.CommandBars("GreenDongForWO").Delete
On Error GoTo 0
End Sub
不知情况,如果可能,弹出框中的命令按钮,即第3,可循环
|
|