在CommandBars(1)上添加,然后就跑会到加载项选项卡上。以下是我的一些实例
1)在sheet1的A:G列我放了一些按钮的基础信息,以便调用
2)添加按钮的过程如下
SubegAddButtons()
OnErrorResume Next
DimIAsInteger,barAsCommandBar,shtAsWorksheet
Setsht=ThisWorkbook.Sheets(1)
Setbar=Application.CommandBars(1)
ForI=1To17
Withbar.Controls.Add(msoControlButton,,,,True)
.OnAction=sht.Range("A1").Offset(I,3).Value
.Style=msoButtonIconAndCaption
.FaceId=sht.Range("A1").Offset(I,4).Value
.Caption=sht.Range("A1").Offset(I,1).Value
.Tag="NewButton"
End With
Next
Setsht=Nothing
Setbar=Nothing
End Sub
3)删除按钮的过程
SubegDeleteButtons()
OnErrorResume Next
DimbarAsCommandBar,ctlAsCommandBarControl
Setbar=Application.CommandBars(1)
Withbar
ForEachctlInbar.Controls
Ifctl.Tag="NewButton"Then
ctl.Visible=False
ctl.Delete
End If
Next
EndWith
Setbar=Nothing
Setctl=Nothing
EndSub