In this screencast I will show you how to make a very simple type of UserForm using
VBA.
So the first thing, because we are going to use VBA, we have to make sure we're on the
developer tab, and then we go into "Visual Basic", which is our VBA environment.
So we go to "Insert UserForm", and up comes this UserForm that we're going to work with,
and I am going to make this a little bit bigger.
One thing that you should notice is that you cannot move the UserForm, it will stay fixed
to this area.
So we're going to make a simple UserForm, we're going to have one text box and one label.
We'll also have two command buttons, a go button, and a quit button.
So the first thing we want to do is rename this form.
We can call it UserForm1 in our code, but I like to give it a name that makes sense.
So here I am going to call it "Test Form", and notice up here that that's its new name.
In addition let's change this caption right here ,and let's just call this "Components".
Now we've set up the UserForm now we're going to use the tool box to put in our labels and
our text box.
So over here is a tool box, and if you notice each one of them has a different what they
call control, and we're going to start by using the Label1.
So what that does is it creates on your UserForm a lablel, and we don't need to change the
name because we're never going to use this in our code, but let's go down here and change
the caption, and what we're going to call this is component.
You can see that the caption has changed.
That's a little small for my taste, so let's go down here to font, click on this, and you'll
see that all the font properties you could want are there.
So let's change the size to sixteen and say OK, and there we have a component label.
The next thing we're going to want to do is put in a text box, and the text box is where
you get your values off the UserForm.
So we click on "Textbox", and we make one, whatever size we want to, and now this is
something that you're going to want to change the name, because this is something you'll
use in the code.
So let's call this component, and it has no caption, we can just leave it the way it is.
One thing you should always do before testing out a UserForm is put a quit button.
If you don't do that you're never going to be able to get out of the UserForm without
having to crash Excel.
So if you notice right here we have what's known as command buttons.
We'll put that there, and we're going to name it, not surprisingly, "Quit", and we're going
to change the caption to say "Quit" and let's again change the font, let's make it italicized,
or oblique.
Make it size 16, OK.
Now we have a quit button.
So what do we next need, we need a command button that will allow us to run a program
from this UserForm, so we put in another command button, and we're going to call this command
button "Go", and we're going to change the caption so that it says "Go" like that, and
now let's change the font, let's make this a good old 18 size.
Alright, so now we have our UserForm but we have nothing for it to do.
That's where we use these command buttons.
Once we double click on it, like this, what you'll notice is you have an area to write
a program, or some lines of code.
So we're going to make this very simple, what we're going to do is assign a variable A,
and we're going to have that equal to component, which was the name of our text box, ".text",
and just to make sure it works we're going to say "MsgBox a".
So what this program should do is take whatever we put in the component text box and then
put out a message box that gives whatever we put there.
Alright now let's go back to our UserForm over here, and we double click there, and
now let's put a program for code for our quit button, and basically all we want to do there
is when we click the quit button have the UserForm go away, and the code for that is
unload, and then the name of your UserForm, and the UserForm we have is what we named
"TestForm".
So now we're ready to go, however how do we run this program?
In order to do that we have to insert a module, and here we have to call it something, so
we'll just call it "Sub showform".
This module will allow us to show the UserForm to the user, and the command for that is the
name of the UserForm.Show.
Alright, so we have our UserForm, we have our module, how is the user going to be able
to access that?
And, the answer is we go back to our Excel environment, Alt+F11, which is what I use,
and we insert some kind of shape, and I'll just put a circle here.
If you recall, and I showed you from a previous screencast, if you right click on the shape
and go to assign macro you'll see that there is our "showform" macro, and we're going to
assign it to that.
Let's see what happens when we run it.
We click on our button and look, up comes the UserForm, and this one we can move around,
and let's just put in the number 5, and say go, and we get our message box that shows
us 5.
So this is just the first step in making a UserForm, in another screencast I'll show
you how to get rid of the UserForm and make it a little bit more complex.
So notice we hit quit and the UserForm goes away.