MsgBox and InputBox Example

advertisement
IE 212: COMPUTATIONAL METHODS FOR INDUSTRIAL ENGINEERING
EXAMPLE FOR LECTURE NOTES #4
“WORKING WITH VARIABLES AND USER INTERFACES”
Program: MsgBox and InputBox
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
Option Explicit
Sub msgBoxExamples()
'Procedure to demonstrate the use of the MsgBox function
'Declare variables
Dim value1 As Integer
Dim value2 As Double
'In the next four examples, the MsgBox function is NOT assigned to a variable.
'Therefore, there is no need to enclose its parameters in parentheses
'All parameters can be added
'Example 1. Displaying a simple message
MsgBox "This is my first message box!"
'Example 2. Displaying a multiple-line message box
'Illustrate the use of the & (concatenation) character
MsgBox "The prompt of this message box" & vbNewLine & _
"spans multiple lines. That is why" & vbNewLine & _
"I need to use special characters"
'Example 3. Displaying a multiple-line message box
'and displaying values taken from the spreadsheet
MsgBox "The value of cell A1 is: " & Range("A1").Value & vbNewLine & _
"The value of cell A2 is: " & Range("A2").Value, , "Values from Sheet1"
'Example 4. Displaying a multiple-line message box
'and displaying values assigned to variables
value1 = Range("A1").Value
value2 = Range("A2").Value
MsgBox "The value of cell A1 is: " & value1 & vbNewLine & _
"The value of cell A2 is: " & value2, , "Values from Variables"
'Assigning the MsgBox function to a variable
'The parameters of the MsgBox function MUST be enclosed in parentheses
'Value of variable can be tested later with an If...Then...Else statement
Dim usrResp As Integer
usrResp = MsgBox("Do you really want to quit?", _
vbYesNo + vbDefaultButton2, _
"Confirm Selection")
'Reporting the value of usrResp
MsgBox "The value of userResp is: " & usrResp
End Sub
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Sub InputBoxExamples()
'Procedure to demonstrate the use of the InputBox function
'Requesting a simple input
'InputBox function MUST always be assigned to a variable
'Pressing the Cancel button of an InputBox function returns an empty string ""
Dim usrStrResp As String
Dim usrDblValue As Double
Dim isUsrStrNum As Boolean
usrStrResp = InputBox("Enter an integer value in the range [0,100]." & vbNewLine & _
"Please do not enter any other value.", _
"Data Input", , 7000, 7000)
'Checking if value entered by user can represent a number
isUsrStrNum = IsNumeric(usrStrResp)
'Use a MsgBox to display the value, the variable type, and
'whether or not the value entered by user can represent a number
MsgBox "Value: " & usrStrResp & vbNewLine & _
"Type: " & VarType(usrStrResp) & vbNewLine & _
"Is this a number?: " & isUsrStrNum
'Casting String value into a value of type Double
usrDblValue = CDbl(usrStrResp)
'Use a MsgBox to display the value, the variable type, and
'whether or not the value entered by user can represent a number
MsgBox "Value: " & usrStrResp & vbNewLine & _
"Type: " & VarType(usrDblValue) & vbNewLine & _
"Is this a number?: " & isUsrStrNum
End Sub
2
Download