In this screencast we're going to show you how to use the check box control for user
forms in VBA.
So as always when we need to go to VBA we click on the developer tab and go to visual
basic.
We can also just go Alt+F11, and as I showed you earlier we're going to insert a user form,
we're going to rename the user form as check box, and we're going to change its caption
so it says check box.
The next thing I am going to do before I even put anything else on there is put a quit button,
so we'll call this quit, and we'll give it a caption, it says "Quit", and we're going
to change its font to 16.
In addition we're going to copy this so we use the command Ctrl+C, click over here, Ctrl+V,
so it copied a command button, and now this one we will call go and change its caption
to go, and notice that we don't have to change the font once we copied it, the font stayed
in there.
So let's go to our tool box and over here is our check box.
So what we're going to have the user do is choose between methane, ethane, and propane.
So here's our first one, our first checkbox, and let's name that methane, and let's change
that caption to say "Methane", and finally let's change the font here to be 14.
OK, we're going to click on it, Ctrl+C, and Ctrl+V, which copies it, and let's make ourselves
a little bit more room here.
Notice you can select more than one thing and move it, and then we're going to go Ctrl+V,
which gives us our final check box.
So this one is methane, we've already taken care of it, this one we're going to name ethane,
and we're going to change the caption to say "Ethane", and finally this one we're going
to call it propane, and change the caption to say "propane".
Now if you notice the three of these are not lined up, and let's say we want them to all
be lined up like methane.
So you go over here into the properties and you look down to see where it says "Left",
and over here is says 42, so for each one of these we want the left, which is 36 here,
to be 42, and notice what happens, and then we go to the propane, where its left is 36 and
we make it 42.
So now they're all lined up.
Let's put the code in for "Quit", which is unload, and remember we call this "checkbox",
go back to our user form, and now let's put in some code for our "Go" button.
So the syntax that we use is "If methane", and it is a if/then, which basically means
if methane is checked then, and we'll just say message box "You checked methane".
Now what if it's ethane?
So we use the word ElseIf, notice it's one word.
So if we check ethane it'll say "ElseIf ethane Then" "Msgbox 'You checked ethane'".
We can do the same thing for propane and say "Elseif", but there's no other choices, so
all we have to write is "Else Msgbox 'You checked propane'" and then we always have
to finish it with an "End If".
So let's see how our program looks.
One of the things you can do if you don't want to go back to Excel or insert a module
while you're checking your user form you go up to this run button here and say "Run Sub
or User Form".
So we're going to run our user form and we're going to check methane, says go, "You checked
methane".
We're going to uncheck this and check ethane, "You checked Ethane", and finally we want
to check all three of these, "You checked propane".
There's one issue with this program however, what happens if you check more than one, well
it takes the very first one, or what happens if you don't check any of them, then it takes
the last one since you had that as your only final choice.
So in the next screencast I'll show you how to get rid of this problem.