Pages

How to make a userform in Excel do something.

     Let's do something simple. First, create a userform and add a button. Buttons are named by default "CommandButton1" so I'll use that name for the code that I supply. If you want to change the names of your buttons (you will want to change the names of your buttons so that you will be able to keep track of which button does what) use the properties window ( View > Properties Window OR F4).
 
     Once your button is on your form, double click it. This will take you to your code and automatically create a sub for when your button is clicked. Note: CommandButton1_Click()
 
Copy and paste the following code between your "CommandButton1_Click()" sub and end sub lines:
 
' 'Dim' declares "YourMessage" as a string type variable.
Dim YourMessage As String
 
'The line below will create a pop up input box that will ask you for a message. It will
'set your "YourMessage" variable to whatever you type.
YourMessage = InputBox("What do you want to use for your message?", "Message")
 
'Then Excel will take the message that you stored in your "YourMessage" variable
'and display it in a message box.
MsgBox (YourMessage)
 
'Congratulations on making your button do something.

No comments:

Post a Comment

Thanks for leaving a comment.