Excel2007如何通过VBA实现自定义选项卡

2020-05-17 教育 175阅读

在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

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com