Excel自定义选项卡添加宏命令
在CommandBars(1)上添加,然后就跑会到加载项选项卡上。以下是我的一些实例
1)在sheet1的A:G列我放了一些按钮的基础信息,以便调用
2)添加按钮的过程如下
Sub egAddButtons()
On Error Resume Next
Dim I As Integer, bar As CommandBar, sht As Worksheet
Set sht = ThisWorkbook.Sheets(1)
Set bar = Application.CommandBars(1)
For I = 1 To 17
With bar.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
Set sht = Nothing
Set bar = Nothing
End Sub
3)删除按钮的过程
Sub egDeleteButtons()
On Error Resume Next
Dim bar As CommandBar, ctl As CommandBarControl
Set bar = Application.CommandBars(1)
With bar
For Each ctl In bar.Controls
If ctl.Tag = "NewButton" Then
ctl.Visible = False
ctl.Delete
End If
Next
End With
Set bar = Nothing
Set ctl = Nothing
End Sub