Macro for a button by code


Posted by Birtz on September 24, 1999 1:24 AM

I must write a macro that assign another macro to a personalized button in a command bar. How can I do ?

Birtz



Posted by Ivan Moala on September 24, 1999 5:40 AM

Birtz
Assuming you know the commandbar name
excel'97


sub CreateCommandBar()
On Error Resume Next
Set MyCBar = Application.CommandBars("Your CB Name")
On Error GoTo 0
If MyCBar Is Nothing Then
Set MyCBar = Application.CommandBars.Add(Name:="Data Viewer")
For i = 1 To Your# of buttons
MyCBar.Controls.Add Type:=msoControlButton
Next i
End If

With MyCBar.Controls(1)
.Caption = "yourCaption if any"
.OnAction = "Your Macro To Run"
'This is where you assign the macro
.FaceId = 1826'Substitute # if you know the face ID#
.TooltipText = "Your tip to display while Cur is hovered over the button"
End With
'Continue to go through
'your button list here

With MyCBar.Controls(2)
'etc

MyCBar.Enabled = True
MyCBar.Visible = True
end sub