首页
/
每日頭條
/
科技
/
vba實現展開菜單
vba實現展開菜單
更新时间:2026-08-05 21:29:13

VBA中自定義菜單命令組、自定義工具欄主要是利用CommandBars對象的方法進行添加,添加的自定義菜單命令組和自定義工具欄将出現在“加載項”選項卡中。

1 自定義菜單命令組

Sub AddCommandbars()

Dim myBarPopup As CommandBarPopup

Dim ArrOne As Variant

Dim ArrTwo As Variant

Dim ArrThree As Variant

Dim i As Byte

On Error Resume Next

'定義各命令按鈕的标題;

ArrOne = Array("多工作簿查找", "創建工作表目錄", "設置頁眉頁腳")

'定義各命令按鈕的圖标ID;

ArrTwo = Array(281, 283, 285)

ArrThree = Array("FormOpen", "PERSONAL.XLSB!創建工作表目錄", "PERSONAL.XLSB!設置頁眉頁腳")

'CommandBars對象可以使用.controls.Add方法新建“加載項”的“菜單命名”組;

With Application.CommandBars("worksheet menu bar")

'重置内置命令欄,避免重複添加;

.Reset

'聲明彈出式控件

Set myBarPopup = .Controls.Add(msoControlPopup)

With myBarPopup

'設置命令欄控件的标題

.Caption = "controls"

For i = 0 To UBound(ArrOne)

With .Controls.Add(msoControlButton)

.Caption = ArrOne(i)

.FaceId = ArrTwo(i) '控件圖标指定;

.OnAction = ArrThree(i)

End With

Next

End With

End With

Set myBarPopup = Nothing

End Sub

2 自定義工具欄

Sub AddBars()

Dim myBar As CommandBar

Dim ArrOne As Variant

Dim ArrTwo As Variant

Dim ArrThree As Variant

Dim i As Byte

On Error Resume Next

ArrOne = Array("多工作簿查找", "創建工作表目錄", "設置頁眉頁腳")

ArrTwo = Array(9893, 284, 9590)

ArrThree = Array("FormOpen", "PERSONAL.XLSB!創建工作表目錄", "PERSONAL.XLSB!設置頁眉頁腳")

Application.CommandBars("MyToolBar").Delete

'CommandBars對象可以使用Add方法新建“加載項”的“自定義工具欄”;

Set myBar = Application.CommandBars.Add(MyToolBar)

With myBar

.Visible = True

For i = 0 To UBound(ArrOne)

With .Controls.Add(msoControlButton)

.Caption = ArrOne(i)

.FaceId = ArrTwo(i)

.OnAction = ArrThree(i)

.Style = msoButtonIconAdnCaptionBelow

End With

Next

End With

Set myBar = Nothing

End Sub

3 工作薄打開時自動執行定義的過程

Private Sub Workbook_Open()

frmFind.Show

Call AddCommandbars

Call AddBars

End Sub

vba實現展開菜單(自定義菜單命令組)1

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
電池沒有問題手機耗電快怎麼解決
電池沒有問題手機耗電快怎麼解決
本文編輯今日頭條作者維權騎士簽約用戶:小俊技術分享獨家原創制作未經授權嚴禁轉載,發現抄襲者将進行全網維權投訴分享生活小妙招,享受科技新生活!大家好,歡迎來到今天的知識分享!我是你們的好朋友小俊!我們在使用手機的過程中,經常會遇到手機掉電快、...
2026-08-05
一般一台手機的使用壽命有多長
一般一台手機的使用壽命有多長
現在手機的品牌真的是各種各樣,國産的以及進口的等等品牌都非常多。現在的人們使用手機特别的普遍,而且很多年輕人都是拿兩個手機。手機的功能現在也非常的多,手機的樣式以及尺寸大小,外觀屏幕也是多種多樣。一台手機的使用壽命有多長?每個人家裡應該都有...
2026-08-05
自動斷電電動車充電器推薦
自動斷電電動車充電器推薦
對于有車一族的朋友來說,車載無線充電器應該都不會陌生,這是專門安裝在車内的産品,可以為我們的手機進行無線充電使用,尤其對于一些經常要旅行或者跑長途的朋友來說,完全就是神器一枚,将手機放置在上面,可以直接無線充電,非常的方便,但是目前市面上的...
2026-08-05
wifi密碼防蹭網
wifi密碼防蹭網
都知道很多情況下,我們在家或者到了某個地方,用了很多wifi連接工具終于讓手機有了網絡啦。這是個前提,畢竟,沒有wifi信号,一切都是是白搭~那麼這時候怎麼讓電腦也連上這個wifi呢?很多人會以為下載一個PC版的萬能鑰匙就可以了。其實那時候...
2026-08-05
了解三分鐘
了解三分鐘
了解三分鐘?在第7節,我們讨論了借助于單片機和傳感器,電腦也能獲取環境參數,例如室内的溫度和濕度等信息,接下來我們就來聊聊關于了解三分鐘?以下内容大家不妨參考一二希望能幫到您!了解三分鐘在第7節,我們讨論了借助于單片機和傳感器,電腦也能獲取...
2026-08-05
Copyright 2023-2026 - www.tftnews.com All Rights Reserved