Assigning a macro to a command button

J

John Kramer

I've been trying to assign a macro to a command button.
I've looked at the help menu, but it tells me to right
click on the button and select assign macro. But that
doesn't exist. I have the command button on the work
sheet. Once I do get this figured out, can I protect the
work sheet except for the buttons? They will be used for
sorting the data, and will that data sort eventhough the
sheet is protected? But first how do I assign the button
and have it work?

John
 
B

Bob Phillips

John,

You have probably got a command button from the control toolbox whereas the
help you are reading refers to the forms command button.

Control toolbox controls trigger events rather than have macros assigned to
them. For instance, this click event code will throw up the ubiquitous
"Hello world" message when the command button is clicked.

Private Sub CommandButton1_Click()
msgbox "Hello world"
End Sub

To add this code, right click on the sheet name tab, and select View Code
from the menu. Enter the code in the code pane shown.

You can either include your code in that click event code, or call you macro
from there.
 
J

John Kramer

Bob

I did find that using the form button was the one. And I
did figure out how to have the buttons do sorts and still
have the sheet protected. What I did was have the
buttons unlocked. So with the sheet protected, when you
click on one of the buttons. It unprotects, does the
sort, then protects the sheet again. It works pretty
slick. Thanks for the help.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top