Option Explicit
Private Sub Workbook_Activate()
With Application.CommandBars("Cell")
On Error Resume Next
.Controls("macro 1").Delete
.Controls("macro 2").Delete
.Controls("macro 3").Delete
.Controls("macro 4").Delete
On Error GoTo 0
With .Controls.Add(Type:=msoControlPopup, temporary:=True)
.Caption = "invoice"
If ActiveSheet.Name = "Sheet1" Then
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "macro 1"
.OnAction = "macro1"
End With
ElseIf ActiveSheet.Name = "Sheet1" Then
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "macro 2"
.OnAction = "macro2"
End With
End If
End With
End With
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
End Sub
Private Sub Workbook_Deactivate()
With Application.CommandBars("Cell").Controls("invoice")
On Error Resume Next
.Controls("macro 1").Delete
.Controls("macro 2").Delete
.Controls("macro 3").Delete
.Controls("macro 4").Delete
On Error GoTo 0
End With
End Sub
Private Sub Workbook_Deactivate()
With Application.CommandBars("Cell")
On Error Resume Next
.Controls("invoice").Delete
On Error GoTo 0
End With
End Sub
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
With Application.CommandBars("Cell").Controls("invoice")
If Sh.Name = "Sheet1" Then
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "macro 1"
.OnAction = "macro1"
End With
ElseIf Sh.Name = "Sheet2" Then
With .Controls.Add(Type:=msoControlButton, temporary:=True)
.Caption = "macro 2"
.OnAction = "macro2"
End With
End If
End With
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
With Application.CommandBars("Cell").Controls("invoice")
On Error Resume Next
If Sh.Name = "Sheet1" Then
.Controls("macro 1").Delete
.Controls("macro 2").Delete
ElseIf Sh.Name = "Sheet2" Then
.Controls("macro 3").Delete
.Controls("macro 4").Delete
End If
On Error GoTo 0
End With
End Sub
'This is workbook event code.
'To input this code, right click on the Excel icon on the worksheet
'(or next to the File menu if you maximise your workbooks),
'select View Code from the menu, and paste the code
--
---
HTH
Bob
(there's no email, no snail mail, but somewhere should be gmail in my addy)