Message Box - Capturing User's Input

Excel VBA for Beginners 10 - Interacting with users
4 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.87
List Price:  €120.23
You save:  €34.35
£74.39
List Price:  £104.15
You save:  £29.76
CA$138.18
List Price:  CA$193.46
You save:  CA$55.28
A$153.39
List Price:  A$214.75
You save:  A$61.36
S$128.92
List Price:  S$180.49
You save:  S$51.57
HK$779.80
List Price:  HK$1,091.76
You save:  HK$311.95
CHF 80.49
List Price:  CHF 112.70
You save:  CHF 32.20
NOK kr1,009.67
List Price:  NOK kr1,413.58
You save:  NOK kr403.91
DKK kr641.01
List Price:  DKK kr897.45
You save:  DKK kr256.43
NZ$170.65
List Price:  NZ$238.92
You save:  NZ$68.26
د.إ367.21
List Price:  د.إ514.11
You save:  د.إ146.90
৳12,159.63
List Price:  ৳17,023.97
You save:  ৳4,864.33
₹8,813.74
List Price:  ₹12,339.59
You save:  ₹3,525.84
RM422.70
List Price:  RM591.80
You save:  RM169.10
₦152,684.73
List Price:  ₦213,764.73
You save:  ₦61,080
₨28,172.18
List Price:  ₨39,442.18
You save:  ₨11,270
฿3,233.20
List Price:  ฿4,526.62
You save:  ฿1,293.41
₺4,116.73
List Price:  ₺5,763.58
You save:  ₺1,646.85
B$546.03
List Price:  B$764.47
You save:  B$218.43
R1,779.46
List Price:  R2,491.32
You save:  R711.85
Лв167.77
List Price:  Лв234.89
You save:  Лв67.11
₩139,375.48
List Price:  ₩195,131.24
You save:  ₩55,755.76
₪336.41
List Price:  ₪470.99
You save:  ₪134.57
₱5,711.89
List Price:  ₱7,996.87
You save:  ₱2,284.98
¥14,830.67
List Price:  ¥20,763.54
You save:  ¥5,932.86
MX$1,876.23
List Price:  MX$2,626.81
You save:  MX$750.57
QR364.02
List Price:  QR509.64
You save:  QR145.62
P1,342.46
List Price:  P1,879.50
You save:  P537.04
KSh12,918.70
List Price:  KSh18,086.70
You save:  KSh5,168
E£4,854.20
List Price:  E£6,796.08
You save:  E£1,941.87
ብር14,292.55
List Price:  ብር20,010.15
You save:  ብር5,717.59
Kz91,186.39
List Price:  Kz127,664.59
You save:  Kz36,478.20
CLP$96,831.31
List Price:  CLP$135,567.71
You save:  CLP$38,736.40
CN¥713
List Price:  CN¥998.24
You save:  CN¥285.23
RD$6,296.92
List Price:  RD$8,815.94
You save:  RD$2,519.02
DA12,990.59
List Price:  DA18,187.34
You save:  DA5,196.75
FJ$226.12
List Price:  FJ$316.58
You save:  FJ$90.46
Q765.93
List Price:  Q1,072.34
You save:  Q306.40
GY$20,890.84
List Price:  GY$29,248.01
You save:  GY$8,357.17
ISK kr12,333.76
List Price:  ISK kr17,267.76
You save:  ISK kr4,934
DH908.31
List Price:  DH1,271.67
You save:  DH363.36
L1,670.58
List Price:  L2,338.87
You save:  L668.29
ден5,277.34
List Price:  ден7,388.49
You save:  ден2,111.14
MOP$802.43
List Price:  MOP$1,123.43
You save:  MOP$321
N$1,761.48
List Price:  N$2,466.14
You save:  N$704.66
C$3,674.73
List Price:  C$5,144.77
You save:  C$1,470.03
रु14,082.30
List Price:  रु19,715.79
You save:  रु5,633.48
S/353.21
List Price:  S/494.51
You save:  S/141.30
K416.61
List Price:  K583.27
You save:  K166.66
SAR375.17
List Price:  SAR525.25
You save:  SAR150.08
ZK2,374.72
List Price:  ZK3,324.71
You save:  ZK949.98
L436.04
List Price:  L610.48
You save:  L174.43
Kč2,099.65
List Price:  Kč2,939.59
You save:  Kč839.94
Ft33,821.21
List Price:  Ft47,351.04
You save:  Ft13,529.83
SEK kr945.93
List Price:  SEK kr1,324.34
You save:  SEK kr378.41
ARS$136,092.68
List Price:  ARS$190,535.20
You save:  ARS$54,442.52
Bs690.01
List Price:  Bs966.04
You save:  Bs276.03
COP$400,414.31
List Price:  COP$560,596.05
You save:  COP$160,181.74
₡50,481.69
List Price:  ₡70,676.39
You save:  ₡20,194.69
L2,617.73
List Price:  L3,664.93
You save:  L1,047.20
₲721,217.97
List Price:  ₲1,009,734.02
You save:  ₲288,516.04
$U4,006.62
List Price:  $U5,609.43
You save:  $U1,602.81
zł365.33
List Price:  zł511.47
You save:  zł146.14
Already have an account? Log In

Transcript

Hey guys welcome back. So in this video we're going to learn how to capture the user's input on a message box. So we're going to learn how to capture the answer of a user if the user clicks on a certain button on the message box then I need to take some actions based on that so we'll learn how to do that. So basically here the message box buttons each one of them has a value so the VB okay the ok button has a certain value the cancel has another value to abort is three or tries four and so on and so forth. So basically, the idea is that we will assign these values to a variable and then we will put an if statement testing what the value is and if the value for example is one or VB Okay, then we will do this and this and that if the value is two, then we will do this and this and that, and so on and so forth.

So let's have an example. Okay, guys, so I've created a simple message box that asks the user Very simple question. Do you love chocolate? And it will have yes and no buttons, and it's going to be a VB question and title of chocolate. So if you run that the user will have this question. And he can answer with either yes or no.

So if the answer is either yes or no, at the moment, nothing will happen. So to make something happen, we need to actually assign the answer of the message box to a variable. And for the data type of the variable, we will have several options. So let's create a so I'll create a variable named answer and the data type for it. This is the one I prefer is VB message box result. And VB message box result is basically an enumeration.

And this is a fancy word However, all it means is that it's just a list of values having constants assigned to them. So now we need to assign the message box to the variable answer. And you can see here when I read equals, I get a list of the different buttons that the user can click, but we won't use this list. Right now, and if I just click away I will get an error as you can see here. The reason for that is that if you write the message box on the right hand side of an assignment operator like the equal sign, you need to put the arguments between brackets. Okay?

So this is something in the VBA syntax. So now we've assigned the message box to the answer variable and so now whatever answer the user gives us will be stored in the variable called answer. Okay, so now we need to put an if statement so if answer equals For example, here we've got yes and no buttons only so if answer equals VBS, then message box, in this case, just going to be a simple message box. Great. I love chocolate too. And elsif answer is equal to VB No.

Then message box. Why not? Chocolate is delicious. And if Okay, so So now based on the user's answer, he will get one of these two message boxes. So if we run this code here, do you love chocolate? If I answer Yes, great.

I love chocolate too. And if I run it again, do you love chocolate? No. Why not? Chocolate is delicious. So based on the user's answer, you can take any kind of actions.

If you put an if statement, you can put any lines of code here on the if statement and do whatever you want. You could also instead of VB yes and VB no use their corresponding values. So instead of VBS, you could put a six and VB No, you could put a seven here and you can use their corresponding values and this will work too. You could also change the data type to an integer and this will work to with the numbers and also you could write equals VBS and VB no And this works as well. However, the advantage of using a VB message box result is that you do get the drop down menu while you're typing the variable. So when you write answer equals, you start getting the list and you don't need to worry about remembering the answers.

Okay, so this is why I'd recommend using the VB message box result data type in this case, so thank you 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.