Excel

Simple Floating Toolbar

Ease of Use

Intermediate

Version tested with

2003 

Submitted by:

lucas

Description:

Create a simple floating toolbar specific to the Excel file on file open. 

Discussion:

This macro creates a two button floating toolbar when the document is opened and is temporary so there is no need to delete it when the document closes. It's fairly easy to configure and assign macros and you can easily add more buttons as needed. This is marked as intermediate only because you will have to change the macros(in the code) associated with the buttons to suite your needs. 

Code:

instructions for use

			

Add this bit of code To the Thisworkbook module: Option Explicit Private Sub Workbook_Open() AddNewToolBar End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) DeleteToolbar End Sub .................................................................................................... Add all of the following code To a standard module: Option Explicit Sub AddNewToolBar() ' This procedure creates a new temporary toolbar. Dim ComBar As CommandBar, ComBarContrl As CommandBarControl On Error GoTo ErrorHandler ' Create a new floating toolbar and make it visible. On Error Resume Next 'Delete the toolbar if it already exists CommandBars("My Toolbar").Delete Set ComBar = CommandBars.Add(Name:="My Toolbar", Position:= _ msoBarFloating, Temporary:=True) ComBar.Visible = True ' Create a button with text on the bar and set some properties. Set ComBarContrl = ComBar.Controls.Add(Type:=msoControlButton) With ComBarContrl .Caption = "Macro1" .Style = msoButtonCaption .TooltipText = "Run Macro1" 'the onaction line tells the button to run a certain macro .OnAction = "Macro1" End With ' Create a button with an image on the bar and set some ' properties. Set ComBarContrl = ComBar.Controls.Add(Type:=msoControlButton) With ComBarContrl 'the facId line will let you choose an icon ' If you choose to use the faceId then the caption is not displayed .FaceId = 1000 .Caption = "Icon Button" .TooltipText = "Run Macro2" 'the onaction line tells the button to run a certain macro .OnAction = "Macro2" End With Exit Sub ErrorHandler: MsgBox "Error " & Err.Number & vbCr & Err.Description Exit Sub End Sub Sub Macro1() MsgBox "You have clicked a button to run Macro1" End Sub Sub Macro2() MsgBox "You have clicked a button to run Macro2" End Sub 'Code to delete the toolbar called by workbook close: Sub DeleteToolbar() On Error Resume Next CommandBars("My Toolbar").Delete End Sub

How to use:

  1. In Excel open the Visual Basic Editor (Alt+F11) and select the ThisWorkbook Module. In the Code pane on the right add the appropriate code from above.
  2. Insert a module and copy the rest of the code to the standard module.
  3. Save the workbook and close it.
 

Test the code:

  1. Open the file you have saved and your toolbar will be created. When you close the workbook the toolbar is removed until the next time you open the file.
 

Sample File:

Toolbar.zip 9.83KB 

Approved by mdmackillop


This entry has been viewed 599 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express