You will need to move your code to a macro in a normal module. Then add a new normal module and paste this in - you need to alter the caption and macro name constants at the top as necessary:
Option Explicit
' this is the name of the toolbar - you can leave this as is or change it
Const mcstrTOOLBAR_NAME As String = "MyHyperlinkButton"
' this is the button caption - change as required
Const mcstrBUTTON_CAPTION As String = "Click me"
' this is the name of the macro that the button will run - change as needed
Const mcstrBUTTON_MACRO As String = "ClickHyperlink"
Sub AddMenu()
Dim cbr As CommandBar, ctl As CommandBarControl
On Error Resume Next
Application.CommandBars(mcstrTOOLBAR_NAME).Delete
On Error GoTo err_handle
' create toolbar
Set cbr = CommandBars.Add(mcstrTOOLBAR_NAME, , , True)
' add button to it
Set ctl = cbr.Controls.Add(msoControlButton, , , , True)
With ctl
.Caption = mcstrBUTTON_CAPTION
.OnAction = mcstrBUTTON_MACRO
.Style = msoButtonCaption
End With
' position toolbar and show it
With cbr
.Position = msoBarTop
.Visible = True
End With
Exit Sub
err_handle:
MsgBox Err.Description
End Sub
Sub RemoveMenu()
On Error Resume Next
Application.CommandBars(mcstrTOOLBAR_NAME).Delete
End Sub
Sub HideMenu()
On Error Resume Next
Application.CommandBars(mcstrTOOLBAR_NAME).Visible = False
End Sub
Sub ShowMenu()
On Error Resume Next
Application.CommandBars(mcstrTOOLBAR_NAME).Visible = True
If Err.Number <> 0 Then AddMenu
End Sub
then in the ThisWorkbook module you need code like this:
Option Explicit
Private Sub Workbook_Activate()
ShowMenu
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
RemoveMenu
End Sub
Private Sub Workbook_Deactivate()
HideMenu
End Sub
Private Sub Workbook_Open()
AddMenu
End Sub
Bookmarks