Escolar Documentos
Profissional Documentos
Cultura Documentos
Join Strings
Code:
Result:
Left
Code:
MsgBox Left(text, 4)
Result:
1
Right
To extract the rightmost characters from a string, use Right. We can directly insert text in a
function as well.
Code:
Result:
Len
Code:
Result:
2
Note: space (position 8) included!
Instr
Code:
Result:
Mid
Code:
Result:
3
Note: started at position 9 (t) with length 4.
Sub hjk()
Dim x As Integer
x = Range("A1").Value
x=x+1
Range("A1").Value = x
End Sub
Sub Exercise()
Beep
End Sub
Sub Exercise()
Dim FirstName As Variant
Dim LastName As String
Dim FullName As String
FirstName = "William"
LastName = "Sansen"
FullName = LastName + ", " & FirstName
4
ActiveCell = "Full Name: " & FullName
End Sub
Sub hjk()
Number = 78
Character = Chr(Number)
ActiveCell = "The ASCII character of " & Number & " is " & Character
End Sub
Sub hjk()
Number = 358
Character = ChrW(Number)
ActiveCell = "The ASCII character of " & Number & " is " & Character
End Sub
Sub Exercise()
Dim Item As String
Dim Length As Integer
Item = "Television"
Length = Len(Item)
5
ActiveCell = "The number of characters in """ & Item & """ is " & Length
End Sub
Sub hjk()
DateHired = #1/4/2005#
ActiveCell = CStr(DateHired)
End Sub
Sub hjk()
Number = 1450.5 / 2
ActiveCell = Str(Number)
End Sub
Sub hjk()
Number = 28645
ActiveCell = Hex(Number)
End Sub
Sub hjk()
6
Dim Number As Double
Number = 28645
ActiveCell = Oct(Number)
End Sub
Sub hjk()
ActiveCell = UCase(ProgrammingEnvironment)
End Sub
Sub hjk()
ProgrammingEnvironment = "BALASUDHAKARHERO"
ActiveCell = LCase(ProgrammingEnvironment)
End Sub
Sub Exercise()
Dim Process As String
Process = "learning"
ActiveCell = "To " & Left(Process, 5) & " is to gain understanding"
End Sub
This RESULT
7
Sub hjk()
StrValue = "Républic"
StrRev = StrReverse(StrValue)
End Sub
Sub hjk()
End Sub
8
The Buttons of a Message Box
The Buttons argument specifies what button(s) should display on the message box. There are
different kinds of buttons available and the VBA language. Each button uses a constant integer
as follows:
When calling the MsgBox() function and specifying the button, you can use one of the above
constant numeric values. Here is an example that displays the Yes and the No buttons on the
message box:
Sub Exercise()
ActiveCell = MsgBox("Your logon credentials have been checked " & _
"and your application has been approved: " & _
"Congratulations!" & vbCrLf & _
"Before leaving, would you like " & _
"to take our survey survey now?", vbYesNo)
End Sub
Besides the buttons, to enhance your message box, you can display an icon in the left section of
the message box. To display an icon, you can use or add a member of the MsgBoxStyle
enumeration. The members that are meant to display an icon are:
vbCritical 16
9
vbQuestion 32
vbExclamation 48
vbInformation 64
Sub hjk()
End Sub
The MsgBox() function can be used to return a value. This value corresponds to the button the
user clicked on the message box. Depending on the buttons the message box is displaying, after
the user has clicked, the MsgBox() function can return a value. The value can be a member of
the MsgBoxResult enumeration or a constant numeric value recognized by the Visual Basic
language. The value returned can be one of the following values:
10
vbNo 7
Introduction
The Visual Basic language provides a function that allows you to request information from the
user who can type it in a text field of a dialog box. The function used to accomplish this is called
InputBox and its basic syntax is:
The most basic piece of information you can provide to the InputBox() function is referred to as
the prompt. It should be a string that the user will read and know what you are expecting. Here is
an example:
Sub Exercise()
InputBox("Enter your date of birth as mm/dd/yyyy")
End Sub
Upon reading the message on the input box, the user is asked to enter a piece of information. The
type of information the user is supposed to provide depends on you, the programmer. Therefore,
there are two important things you should always do. First you should let the user know what
type of information is requested. Is it a number (what type of number)? Is it a string (such as the
name of a country or a customer's name)? Is it the location of a file (such as C:\Program
Files\Homework)? Are you expecting a Yes/No True/False type of answer (if so, how should the
user provide it)? Is it a date (if it is a date, what format is the user supposed to enter)? These
questions mean that you should state a clear request to the user and specify what kind of value
you are expecting. A solution, also explicit enough, consists of providing an example to the user.
The second argument to the InputBox() function allows you to optionally specify the title of the
input box. This is the string that would appear on the title bar. Since this is an optional argument,
11
if you don't pass it, the input box would display the name of the application. Otherwise, to
display your own title bar, pass the Title argument.
Sub Exercise()
ActiveCell = InputBox("Please enter your date of birth as mm/dd/yyyy", _
"Student Registration")
End Sub
Notice that the caption is now customized instead of the name of the application. The caption can
also be a string created from an expression or emanating from a variable or value.
Sometimes, even if you provide an explicit request, the user might not provide a new value but
click OK. The problem is that you would still need to get the value of the text box and you might
want to involve it in an expression. You can solve this problem and that of providing an example
to the user by filling the text box with a default value. To support this, the InputBox() function
provides the third argument.
To present an example or default value to the user, pass a third argument to the InputBox()
function. If you want to use this argument to provide an example the user can follow, provide it
with the right format. Here is an example:
Sub Exercise()
ActiveCell = InputBox("Enter Student Name:", _
"Student Registration", "John Doe")
End Sub
12
Notice that, when the input box displays with a default value, the value is in the text box and the
value is selected. Therefore, if the value is fine, the user can accept it and click OK. Another way
you can use the default value is to provide a value the user can accept; that is, the most common
or most likely value the user would enter. Here is an example:
Sub Exercise()
ActiveCell = InputBox("Enter Birth State:", _
"Student Registration", "VA")
End Sub
Once again, notice that the user can just accept the value and click OK or press Enter.
By default, when the input box comes up, it displays in the middle of the screen. If you want,
you can specify where the input box should be positioned when it comes up. To assist you with
this, the InputBox() function is equipped with a fourth and a fifth arguments. The fourth
argument specifies the x coordinate of the input box; that is, the distance from its left border to
the left border of the monitor. The fifth argument specifies the distance from the top border of
the input box to the top border of the monitor.
When the input box displays, after typing a value, the user would click one of the buttons: OK or
Cancel. If the user clicks OK, you should retrieve the value the user would have typed. It is also
your responsibility to find out whether the user typed a valid value. Because the InputBox()
function can return any type of value, it has no mechanism of validating the user's entry. To
13
retrieve the value of the input box dialog when the user clicks OK, you can get the returned value
of the InputBox() function.
After being used, the InputBox() function returns a string. Here is an example of getting it:
Sub Exercise()
Dim StudentName As String
You can also get any type of value from an input box. That is, when the InputBox() function
exits, thanks to the flexibility of the Visual Basic language, the compiler can directly cast the
returned value for you. Here is an example:
Sub Exercise()
Dim DateOfBirth As Date
14