Probelm with macros assigned to a custom toolbar

Learner123

New Member
Joined
Sep 3, 2010
Messages
11
I am having a probelm with macros assigned to a custom toolbar. I am using excel 2003.

1. I have created several macros in a workbook named "PLAN"

2. I then created a custom toolbar.

3. Through the customize dialog box, I assign the Macros to the custom tool bar button.

Everything work fine UNTIL...

If I do a SAVE AS for the workbook "PLAN" and call it something else like "PLAN2" and then close "PLAN2" and reopen "PLAN" the name of the macro assigned to the custom button has change

Where as before it was simply "PLAN"
now it is C:\Mydocuments\Test2.xls'!PLAN.
Since I have the workbook "PLAN" open and "PLAN2" is closed, excel now wants to open up "PLAN2" to access the macro assigned to that button.

I am creating a custom toolbar that will go with an excel workbook that I use as a template and will often resave it with different names.

I would like excel not to go looking in other workbooks for these macros since copies of the macros exist in the module attached to the workbook.

Every time I do a Save As the assigned macro gets renamed. I am not interested in making changes to Personal.xls, as other individuals will be using the workbook...

Does anyone have any ideas?
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I am having a probelm with macros assigned to a custom toolbar. I am using excel 2003.

1. I have created several macros in a workbook named "PLAN"

2. I then created a custom toolbar.

3. Through the customize dialog box, I assign the Macros to the custom tool bar button.

Everything work fine UNTIL...

If I do a SAVE AS for the workbook "PLAN" and call it something else like "PLAN2" and then close "PLAN2" and reopen "PLAN" the name of the macro assigned to the custom button has change

Where as before it was simply "PLAN"
now it is C:\Mydocuments\Test2.xls'!PLAN.
Since I have the workbook "PLAN" open and "PLAN2" is closed, excel now wants to open up "PLAN2" to access the macro assigned to that button.

I am creating a custom toolbar that will go with an excel workbook that I use as a template and will often resave it with different names.

I would like excel not to go looking in other workbooks for these macros since copies of the macros exist in the module attached to the workbook.

Every time I do a Save As the assigned macro gets renamed. I am not interested in making changes to Personal.xls, as other individuals will be using the workbook...

Does anyone have any ideas?

Maybe, something like:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Activeworkbook.saveas............PLAN
End Sub
 
Upvote 0
Thanks for your responses!

The code that I am using is:

Code:
Option Explicit
Private Sub Auto_Open()
    'Make a commandbar when this workbook is opened
    CreateMyCommandBar
End Sub
Private Sub Auto_Close()
    
    'Delete a commandbar when this workbook is closed
    DelteMyCommandBar
End Sub
Private Sub CreateMyCommandBar()
    Dim ocb As CommandBarControl
    Dim objCommandBarButton As CommandBarButton
    'reset/delete a previous custom menu before create a new custom menu
    Call DelteMyCommandBar
    Set ocb = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlPopup, Temporary:=True)
    ocb.Caption = "&My Button Name"
    'add a ControlButton to a commandbar
            
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Add Option A"
            .OnAction = "AddOptionA"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Add Option B"
            .OnAction = "AddOptionB"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Add Option C"
            .OnAction = "AddOptionC"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Open SQ"
            .OnAction = "OpenSQ"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Open Comp"
            .OnAction = "OpenComp"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Open ARF"
            .OnAction = "OpenARF"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Business Case - Light"
            .OnAction = "BusinessCase-Light"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Business Case - Full"
            .OnAction = "Business Case-Full"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Project Origin"
            .OnAction = "ProjectOrigin"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Feasibility Study"
            .OnAction = "FeasibilityStudy"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Final Account"
            .OnAction = "FinalAccount"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set objCommandBarButton = ocb.CommandBar.Controls.Add(Type:=msoControlButton, ID:=1)
    With objCommandBarButton
            .Caption = "Menu"
            .OnAction = "Menu"
            .Style = msoButtonIconAndCaption
            .FaceId = 2892
            .BeginGroup = False
    End With
    
    Set ocb = Nothing
    Set objCommandBarButton = Nothing
    
End Sub
Private Sub DelteMyCommandBar()
    On Error Resume Next
    Application.CommandBars("Worksheet Menu Bar").Controls("&My Button Name").Delete
    Err.Clear: On Error GoTo -1: On Error GoTo 0
End Sub
Sub AddOptionA()
   Range("A4").Select
   Sheets("Option A").Visible = True
End Sub
Sub AddOptionB()
    Range("A4").Select
    Sheets("Option B").Visible = True
End Sub
Sub AddOptionC()
    Sheets("Option C").Visible = True
End Sub
Sub OpenSQ()
    Sheets("Status Quo").Visible = True
End Sub
Sub OpenComp()
    Sheets("Options Comparison").Visible = True
End Sub
Sub OpenARF()
   Sheets("Status Quo").Visible = True
   Sheets("ARF").Visible = True
   Sheets("ARF Input").Visible = True
End Sub
Sub OpenBCLite()
    Sheets("Business Case - Light").Visible = True
End Sub
Sub OpenBCFull()
    Sheets("Business Case - Full").Visible = True
End Sub
Sub OpenOrigin()
    Sheets("Project Origin").Visible = True
    
End Sub
Sub OpenFeasibility()
    Sheets("Feasibility Study").Visible = True
    
End Sub
Sub OpenFinalAccount()
    Range("A7").Select
    Sheets("Final Account").Visible = True
    Range("E50").Select
End Sub
Sub Menu()
    Sheets("Menu").Select
    Range("A31").Select
End Sub

However, it continues to reassign (link) the macros in the inital workbook to the newly created workbook.

Any ideas on how to correct this?
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top