Input Box - Capturing a User's Name

Excel VBA for Beginners 10 - Interacting with users
5 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€85.36
List Price:  €119.51
You save:  €34.14
£73.83
List Price:  £103.37
You save:  £29.53
CA$138.47
List Price:  CA$193.86
You save:  CA$55.39
A$151.31
List Price:  A$211.84
You save:  A$60.53
S$128.26
List Price:  S$179.57
You save:  S$51.31
HK$778.85
List Price:  HK$1,090.43
You save:  HK$311.57
CHF 79.67
List Price:  CHF 111.55
You save:  CHF 31.87
NOK kr992.30
List Price:  NOK kr1,389.26
You save:  NOK kr396.96
DKK kr637.29
List Price:  DKK kr892.24
You save:  DKK kr254.94
NZ$168.10
List Price:  NZ$235.35
You save:  NZ$67.24
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,170.03
List Price:  ৳17,038.54
You save:  ৳4,868.50
₹8,808.38
List Price:  ₹12,332.09
You save:  ₹3,523.70
RM421.90
List Price:  RM590.68
You save:  RM168.78
₦151,193.87
List Price:  ₦211,677.47
You save:  ₦60,483.60
₨28,231.96
List Price:  ₨39,525.87
You save:  ₨11,293.91
฿3,178.48
List Price:  ฿4,450
You save:  ฿1,271.52
₺4,128
List Price:  ₺5,779.37
You save:  ₺1,651.36
B$543.26
List Price:  B$760.59
You save:  B$217.32
R1,755.07
List Price:  R2,457.17
You save:  R702.10
Лв167.10
List Price:  Лв233.95
You save:  Лв66.84
₩138,832.84
List Price:  ₩194,371.54
You save:  ₩55,538.69
₪333.50
List Price:  ₪466.92
You save:  ₪133.41
₱5,707.77
List Price:  ₱7,991.11
You save:  ₱2,283.34
¥14,741.11
List Price:  ¥20,638.15
You save:  ¥5,897.03
MX$1,863.42
List Price:  MX$2,608.87
You save:  MX$745.44
QR364.13
List Price:  QR509.80
You save:  QR145.66
P1,340.20
List Price:  P1,876.33
You save:  P536.13
KSh12,913.70
List Price:  KSh18,079.70
You save:  KSh5,166
E£4,818.50
List Price:  E£6,746.10
You save:  E£1,927.59
ብር14,329.41
List Price:  ብር20,061.75
You save:  ብር5,732.33
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$96,707.32
List Price:  CLP$135,394.12
You save:  CLP$38,686.80
CN¥712.05
List Price:  CN¥996.91
You save:  CN¥284.85
RD$6,361.82
List Price:  RD$8,906.81
You save:  RD$2,544.98
DA12,985.60
List Price:  DA18,180.36
You save:  DA5,194.76
FJ$227.15
List Price:  FJ$318.02
You save:  FJ$90.87
Q766.35
List Price:  Q1,072.93
You save:  Q306.57
GY$20,910.82
List Price:  GY$29,275.99
You save:  GY$8,365.16
ISK kr12,241.77
List Price:  ISK kr17,138.97
You save:  ISK kr4,897.20
DH903.67
List Price:  DH1,265.17
You save:  DH361.50
L1,664.84
List Price:  L2,330.84
You save:  L666
ден5,260.13
List Price:  ден7,364.39
You save:  ден2,104.26
MOP$802.03
List Price:  MOP$1,122.88
You save:  MOP$320.84
N$1,755.10
List Price:  N$2,457.21
You save:  N$702.11
C$3,697.17
List Price:  C$5,176.19
You save:  C$1,479.01
रु14,095.46
List Price:  रु19,734.22
You save:  रु5,638.75
S/352.69
List Price:  S/493.78
You save:  S/141.09
K425.96
List Price:  K596.36
You save:  K170.40
SAR375.12
List Price:  SAR525.18
You save:  SAR150.06
ZK2,408.92
List Price:  ZK3,372.58
You save:  ZK963.66
L433.05
List Price:  L606.29
You save:  L173.24
Kč2,082.07
List Price:  Kč2,914.98
You save:  Kč832.91
Ft33,609.14
List Price:  Ft47,054.14
You save:  Ft13,445
SEK kr934.06
List Price:  SEK kr1,307.72
You save:  SEK kr373.66
ARS$141,577.37
List Price:  ARS$198,213.98
You save:  ARS$56,636.61
Bs694.28
List Price:  Bs972.02
You save:  Bs277.74
COP$395,357.19
List Price:  COP$553,515.89
You save:  COP$158,158.69
₡50,492.27
List Price:  ₡70,691.20
You save:  ₡20,198.92
L2,631.87
List Price:  L3,684.73
You save:  L1,052.85
₲719,695.19
List Price:  ₲1,007,602.06
You save:  ₲287,906.86
$U3,993.04
List Price:  $U5,590.42
You save:  $U1,597.37
zł363.60
List Price:  zł509.06
You save:  zł145.45
Already have an account? Log In

Transcript

Okay, guys, so now we're going to have an example for the input box. so we can write input box, what is your name, and that is the prompt argument. And we could put the title as name. And then the default value could be Please enter your name. So this is going to be our input box. So if we run that, you can see here we've got the default value as Please enter your name.

So we're using it to guide the user on what to enter. If we press OK, or press cancel, nothing's going to happen. So I press cancel, nothing happened. Presto, okay, nothing happened, because we still not set up the input box to capture the value that the user inputs. So in order to capture the value that the user inputs, you could either put the value in a cell so say, range a one equals whether user inputs for example, or you could put it in a variable. So this In your name as string, so we're gonna make it a string because the input box actually outputs values as a string.

So this is why we're gonna make the datatype of the variable to be a string. Now we're going to set that variable to be equal to the input box. So your name is equal to input box. And because the input box now is on the right hand side of an assignment operator of the equal sign, we will get an error this way. So we need to put its arguments between brackets. So you can see here now we've got a variable called your name, that whatever the user typed into the input box is going to go into the user name variable, and then we can have a message box, hello, and then a space and concatenate that on to your name.

Okay, so let's try the input box. So if you run that, what is your name? I'm going to enter my name for example. All men, and then click OK. So it's going to display a message Hello, abdomen. Now if we run it as well, and instead of pressing OK, we press Cancel.

Pressing cancel gives us a blank value or puts a blank value into the variable basically. So when you press cancel, we get just hello and blank because when you press cancel, you just get a blank value. Okay, so let's see some examples about dealing with the different scenarios for the user input. So for example, what if the user presses okay with a default value as Please enter your name. So we could have an if statement here, if your name is equal to please enter your name, then we will display a message box invalid value received Please enter your name. Now what if the user enters a blank value or presses cancel, so we could have elsif, your name is equal to blank, then message box name is mandatory, please enter your name.

Okay, so now when we receive an invalid value or a blank value from the user, we are prompting him to enter his name. However, if you run the macro this way, we're going to go through the if statement if the user does not give us a proper value, but then we're going to have the message box Hello, and the improper value, right? So we need to do something to redirect the user to go back and enter his name. So let's run it and see what happens first. So here it says, Please enter your name. So let's say I'm going to press cancel.

So this way, I've given a blank value. So you can see here now that there's a message box that says name is mandatory, please enter your name. So if you're pressing Okay, it's gonna say hello and a blank value, right, because we've got the message box being executed here just after the if statement. So we need something to adjust the flow. So we could have a go to statement. So go to start, for example.

And we'd have this go to statement here as well, okay, and then we'd have a starting point here, start. So this way, the user would be going back to the input box that prompts him to enter his name. So if you run that, and then we would not enter a proper value. So here, if we press OK, so this way we've entered Please enter your name, which is not a proper value. So it says your invalid value received Please enter your name. And then we go back to the input box that says What is your name?

So this way if I enter a proper value mining for example here and we press OK. would go to the Hello, message box. Okay, so this is one way of dealing with improper values or improper inputs that are given by the user. So, thank you very much guys for watching this video and I'll see you on the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.