我刚刚完成了一个VBA项目,现在我想在“加载项”选项卡中为其添加一个按钮。我继承了一个现有的工具栏,因此需要将我的按钮合并到其中。 Demo。我在工具栏中有.xlam文件,用于现有的宏。
在我的项目中,我添加了一个模块,该模块调用要在按钮中运行的所有宏。我不确定这是否是必要步骤,但是不确定here's what it looks like now。我也将宏下载为.xlam文件和.bas文件。
我已经找到了很多资源,可以在“快速访问工具栏”和“开发人员”选项卡中添加宏按钮,但这并不是我所需要的。有关如何将宏添加到“加载项”标签的任何提示或链接?
这是CommandBar
和msoControl
代码:
Sub MyCB()
On Error Resume Next
Application.CommandBars(sCB).Delete
On Error GoTo 0
With Application.CommandBars.Add(sCB, , False, True)
With .Controls.Add(msoControlButton)
.Tag = "Compound"
.Caption = "Compound Summary"
.TooltipText = "Create Compound Summary"
.OnAction = "CreateSummaryReport"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "HPLC Stability Timetable"
.Caption = "HPLC Stability Timetable"
.TooltipText = "Create HPLC Stability Timetable"
.OnAction = "STBLIncubationTime"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "Well Sorting"
.Caption = "Well Sorting"
.TooltipText = "Sort Data Submissions"
.OnAction = "WellSorter"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "CLND Data Preparation"
.Caption = "CLND Data Preparation"
.TooltipText = "Formats and copies CLND data for transfer to YTD"
.OnAction = "CLNDdecider"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "Grav. Caff. YTD Dump"
.Caption = "Grav. Caff. YTD Dump"
.TooltipText = "Dumps grav. caff. data into YTD"
.OnAction = "gravcafffinder"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "Array Assay Platemap to CSV"
.Caption = "Array Assay Platemap to CSV"
.TooltipText = "Converts Array assay platemaps to CSVs"
.OnAction = "AssayPlatemapToCSV"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "Array Submission Setup"
.Caption = "Array Submission Setup"
.TooltipText = "Performs initial setup for Array submission file"
.OnAction = "ArraySubmissionWorksheet"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "Array Cal. Platemap to CSV"
.Caption = "Array Cal. Platemap to CSV"
.TooltipText = "Converts Array calibration platemaps to CSVs"
.OnAction = "CalPlatemapToCSV"
.Style = msoButtonCaption
End With
With .Controls.Add(msoControlButton)
.Tag = "ElogD Report Formatting"
.Caption = "ElogD Report Formatting"
.TooltipText = "Formats ElogD Table for Reporting"
.OnAction = "ElogDreportformat"
.Style = msoButtonCaption
End With
.Protection = msoBarNoCustomize
.Position = msoBarTop
.RowIndex = msoBarRowLast
.Visible = True
End With
End Sub
答案 0 :(得分:0)
这里是创建下拉菜单而不是一堆按钮的方法
Const MENU_NAME As String = "Lab Tools"
Sub CreateMenuTest()
Dim menu As CommandBarPopup
Dim menuItem As Object, opt As Object
Set menu = Application.CommandBars(1).Controls.Add(Type:=msoControlPopup, _
before:=HelpPos(), temporary:=True)
menu.Caption = MENU_NAME
Set menuItem = menu.Controls.Add(Type:=msoControlButton)
menuItem.Caption = "Do Something"
menuItem.OnAction = "DoSomething"
Set menuItem = menu.Controls.Add(Type:=msoControlButton)
menuItem.Caption = "Do Another Thing"
menuItem.OnAction = "DoAnother thing"
'start a sub-menu
Set menuItem = menu.Controls.Add(Type:=msoControlPopup)
menuItem.Caption = "Options..."
Set opt = menuItem.Controls.Add(Type:=msoControlButton)
opt.Caption = "Option A"
opt.OnAction = "DoActionA"
Set opt = menuItem.Controls.Add(Type:=msoControlButton)
opt.Caption = "Option B"
opt.OnAction = "DoActionB"
End Sub
Sub RemoveMenuTest()
Application.CommandBars(1).Controls(MENU_NAME).Delete
End Sub