Variables and Getting In put
Earlier, you used a
MsgBox
to pop up a message on the screen. You can use
a similar box to get in put from your stu dents. The only
dif ference is that the new
dialog box will have a space for your students to type something. We’ll start with
something simple: asking for the stu dent’s name.
Sub YourName()
userName = InputBox(Prompt:="Type your name", _
Ti
tle:="Input Name")
End Sub
There are a few important things about this simple pro cedure. First, pay at-
tention to the space and un derscore at the end of the line. The
last three charac-
ters on the second line are comma, space, and un derscore. Without the space, the
computer won’t recognize the un derscore that fol lows.
The un derscore is a spe-
cial VBA character that tells VBA that what is on the next line is part of this line.
Therefore that entire line could have been written on one line without the
underscore:
userName = InputBox(Prompt:="Type your name", Ti
tle:="Input Name")
The un derscore simply al lows you to di vide long lines so you don’t have to
scroll to the right to see what is on each line. Feel free
to write long lines on one
line or di vide them up among sev eral lines as you see fit.
The next thing that is im portant about this small piece of code is that it uses
a vari able:
userName
. Since we don’t do any thing with the variable at this point,
it
is not terribly in teresting, but we should note a few things about variables.
Variables are places to store in formation. You can think of them as boxes in the
computer’s memory. Un like algebraic vari ables, which represent one (or more
than one)
specific, un changing value in an equation or series of equations, com -
puter variables change values. That is, you can take something out of a box and
put something else into the box. In al gebra,
the equation
Do'stlaringiz bilan baham: