Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add custom buttons 1

Status
Not open for further replies.

denko

Programmer
May 17, 2001
70
0
0
US
How can i assign macros to multiple buttons in excel?
I have a code where i create a toolbar, add a button and assign macro to it, but when i add a second button macro is being assigned to the first button instead of the second.

Dim cbar1, myBar
'
Set cbar1 = CommandBars.Add(Name:="Future Tranx", Position:=msoBarBottom)
cbar1.Visible = True
Set myBar = CommandBars("Future Tranx")
Application.CommandBars("Future Tranx").Controls.Add Type:=msoControlButton, _
ID:=988, Before:=1
Set cbar1 = myBar.Controls(myBar.Controls.Count)

cbar1.Caption = "&Generate Commission Totals"
myBar.Controls("&Generate Commission Totals").Style = msoButtonIconAndCaption
cbar1.BeginGroup = True
cbar1.OnAction = "InsertCommTotals"

Application.CommandBars("Future Tranx").Controls.Add Type:=msoControlButton, _
ID:=455, Before:=2

------does not work------
'cbar1.Caption = "&Update File"
' myBar.Controls("&Update File").Style = msoButtonIconAndCaption
'myBar.BeginGroup = True
'cbar1.OnAction = "Sheet_SaveAs_Date"

Thanks
 
Don't take it personally, but you've got a mess! Try the following re-written procedure:
Code:
Sub CreateCustomCommandBar()
Dim MyCBar As CommandBar
Dim CBarCtrl As CommandBarButton


   Set MyCBar = Application.CommandBars.Add(Name:="Future Tranx", Position:=msoBarBottom)
   MyCBar.Visible = True

   Set CBarCtrl = MyCBar.Controls.Add(Type:=msoControlButton, _
        ID:=988, Before:=1)

   With CBarCtrl
     .Style = msoButtonIconAndCaption
     .Caption = "&Generate Commission Totals"
     .BeginGroup = True
     .OnAction = "InsertCommTotals"
   End With
   
   Set CBarCtrl = MyCBar.Controls.Add(Type:=msoControlButton, _
        ID:=455, Before:=2)

   With CBarCtrl
     .Style = msoButtonIconAndCaption
     .Caption = "&Update File"
     .BeginGroup = True
     .OnAction = "Sheet_SaveAs_Date"
   End With

End Sub


Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top