Sunteți pe pagina 1din 14

STRING FUNCTION WITH MACRO

Join Strings

We use the & operator to concatenate (join) strings.

Code:

Dim text1 As String, text2 As String


text1 = "Hi "
text2 = "Tim"

MsgBox text1 & text2

Result:

Left

To extract the leftmost characters from a string, use Left.

Code:

Dim text As String


text = "example text"

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:

MsgBox Right("example text", 2)

Result:

Len

To get the length of a string, use Len.

Code:

MsgBox Len("example text")

Result:

2
Note: space (position 8) included!

Instr

To find the position of a substring in a string, use Instr.

Code:

MsgBox Instr("example text", "am")

Result:

Note: string "am" found at position 3.

Mid

To extract a substring, starting in the middle of a string, use Mid.

Code:

MsgBox Mid("example text", 9, 4)

Result:

3
Note: started at position 9 (t) with length 4.

Auto Increment Formula

Sub hjk()

Dim x As Integer

x = Range("A1").Value

x=x+1

Range("A1").Value = x

End Sub

Public Sub Beep()

Here is an example of calling it:

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()

Dim Character As String

Dim Number As Integer

Number = 78

Character = Chr(Number)

ActiveCell = "The ASCII character of " & Number & " is " & Character

End Sub

Sub hjk()

Dim Character As String

Dim Number As Long

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()

Dim DateHired As Date

DateHired = #1/4/2005#

ActiveCell = CStr(DateHired)

End Sub

Sub hjk()

Dim Number As Double

Number = 1450.5 / 2

ActiveCell = Str(Number)

End Sub

Sub hjk()

Dim Number As Integer

Number = 28645

ActiveCell = Hex(Number)

End Sub

Sub hjk()

6
Dim Number As Double

Number = 28645

ActiveCell = Oct(Number)

End Sub

Sub hjk()

Dim ProgrammingEnvironment As String

ProgrammingEnvironment = "Visual Basic for Application for Microsoft Excel"

ActiveCell = UCase(ProgrammingEnvironment)

End Sub

Sub hjk()

Dim ProgrammingEnvironment As String

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()

Dim StrValue As String

Dim StrRev As String

StrValue = "Républic"

StrRev = StrReverse(StrValue)

ActiveCell = StrValue & vbCrLf & StrRev

End Sub

Sub hjk()

MsgBox ("Your logon credentials have been checked." & _

vbCrLf & "To complete your application, please " & _

"fill out the following survey")

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:

Constant Numeric Value Display


vbOKOnly 0
vbOKCancel 1
vbAbortRetryIgnore 2
vbYesNoCancel 3
vbYesNo 4
vbRetryCancel 5

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

This would produce:

The Icon on a Message Box

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:

Icon Constant Numeric Value Description

vbCritical 16

9
vbQuestion 32

vbExclamation 48

vbInformation 64

Sub hjk()

Dim iAnswer As Integer

iAnswer = 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 Or vbQuestion)

End Sub

The Returned Value of a Message Box

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:

If the user click The function returns Numeric Value


vbOK 1
vbCancel 2
vbAbort 3
vbRetry 4
vbIgnore 5
vbYes 6

10
vbNo 7

The Input Box

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:

InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])


Presenting the Message

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

This would produce

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 Title of an Input Box

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.

The title is passed as a string. Here is an example:

Sub Exercise()
ActiveCell = InputBox("Please enter your date of birth as mm/dd/yyyy", _
"Student Registration")
End Sub

This would produce:

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.

The Default Value of an Input Box

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

Here is an example of running the program:

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

Here is an example of running the program:

Once again, notice that the user can just accept the value and click OK or press Enter.

The Location of the Input Box

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.

The Return Value of an Input Box

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

StudentName = InputBox("Enter Student Name:", _


"Student Registration")
MsgBox ("Student Name: " & StudentName)
End Sub

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

DateOfBirth = InputBox("Please enter your date of birth as mm/dd/yyyy", _


"Student Registration")
MsgBox("Date of Birth: " & DateOfBirth)
End Sub

14

S-ar putea să vă placă și