0% found this document useful (0 votes)
1 views

VBA string function

The document provides an overview of string handling in VBA, including the definition of strings and their syntax. It details various string functions such as InStr, LCase, UCase, Left, Right, Mid, and others, along with their syntax and examples. The document serves as a guide for developers to effectively utilize string functions in their VBA programming.

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

VBA string function

The document provides an overview of string handling in VBA, including the definition of strings and their syntax. It details various string functions such as InStr, LCase, UCase, Left, Right, Mid, and others, along with their syntax and examples. The document serves as a guide for developers to effectively utilize string functions in their VBA programming.

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

VBA - Strings

Strings are a sequence of characters, which can consist of either


alphabets, numbers, special characters, or all of them. A variable
is said to be a string if it is enclosed within double quotes " ".

Syntax
variablename = "string"
Examples
str1 = "string" ' Only Alphabets
str2 = "132.45" ' Only Numbers
str3 = "!@#$;*" ' Only Special Characters
Str4 = "Asc23@#" ' Has all the above
String Functions

There are predefined VBA String functions, which help the


developers to work with the strings very effectively. Following are
String methods that are supported in VBA. Please click on each
one of the methods to know in detail.

Sr.N
Function Name & Description
o.

InStr
1
Returns the first occurrence of the specified substring. Search happens from the
left to the right.

InstrRev
2
Returns the first occurrence of the specified substring. Search happens from the
right to the left.

Lcase
3
Returns the lower case of the specified string.

Ucase
4
Returns the upper case of the specified string.

Left
5
Returns a specific number of characters from the left side of the string.

Right
6
Returns a specific number of characters from the right side of the string.
Mid
7
Returns a specific number of characters from a string based on the specified
parameters.

Ltrim
8
Returns a string after removing the spaces on the left side of the specified
string.

Rtrim
9
Returns a string after removing the spaces on the right side of the specified
string.

Trim
10
Returns a string value after removing both the leading and the trailing blank
spaces.

Len
11
Returns the length of the given string.

Replace
12
Returns a string after replacing a string with another string.

Space
13
Fills a string with the specified number of spaces.

StrComp
14
Returns an integer value after comparing the two specified strings.

String
15
Returns a string with a specified character for specified number of times.

StrReverse
16
Returns a string after reversing the sequence of the characters of the given
string.
VBA - Instr
The InStr Function returns the first occurrence of one string within
another string. The search happens from the left to the right.

Syntax
InStr([start,]string1,string2[,compare])
Parameter Description
 Start − An optional parameter. Specifies the starting
position for the search. The search begins at the first
position from the left to the right.
 String1 − A required parameter. String to be searched.
 String2 − A required parameter. String against which
String1 is searched.
 Compare − An optional parameter. Specifies the string
comparison to be used. It can take the following mentioned
values.
o 0 = vbBinaryCompare - Performs Binary Comparison
(Default)
o 1 = vbTextCompare - Performs Text Comparison
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim Var As Variant
Var = "Microsoft VBScript"
MsgBox ("Line 1 : " & InStr(1, Var, "s"))
MsgBox ("Line 2 : " & InStr(7, Var, "s"))
MsgBox ("Line 3 : " & InStr(1, Var, "f", 1))
MsgBox ("Line 4 : " & InStr(1, Var, "t", 0))
MsgBox ("Line 5 : " & InStr(1, Var, "i"))
MsgBox ("Line 6 : " & InStr(7, Var, "i"))
MsgBox ("Line 7 : " & InStr(Var, "VB"))
End Sub
VBA -InString Reverse
The InStrRev function returns the first occurrence of one string
within another string. The Search happens from the right to the
left.

Syntax
InStrRev(string1,string2[,start,[compare]])
Parameter Description
 String1 − A required parameter. String to be searched.
 String2 − A required parameter. String against which
String1 is searched.
 Start − An optional parameter. Specifies the starting
position for the search. The search begins at the first
position from the right to the left.
 Compare − An optional parameter. Specifies the string
comparison to be used. It can take the following mentioned
values.
o 0 = vbBinaryCompare - Performs Binary Comparison
(Default)
o 1 = vbTextCompare - Performs Text Comparison
Example

Add a button and place the following function.

Private Sub Constant_demo_Click()


var = "Microsoft VBScript"
msgbox("Line 1 : " & InStrRev(var,"s",10))
msgbox("Line 2 : " & InStrRev(var,"s",7))
msgbox("Line 3 : " & InStrRev(var,"f",-1,1))
msgbox("Line 4 : " & InStrRev(var,"t",5))
msgbox("Line 5 : " & InStrRev(var,"i",7))
msgbox("Line 6 : " & InStrRev(var,"i",7))
msgbox("Line 7 : " & InStrRev(var,"VB",1))
End Sub
VBA - Lcase
The LCase function returns the string after converting the entered
string into lower case letters.

Syntax
Lcase(String)
Example

Add a button and place the following function inside the same.

Private Sub Constant_demo_Click()


var = "Microsoft VBScript"
msgbox("Line 1 : " & LCase(var))
var = "MS VBSCRIPT"
msgbox("Line 2 : " & LCase(var))
var = "microsoft"
msgbox("Line 3 : " & LCase(var))
End Sub
VBA - UCase
The UCase function returns the string after converting the entered
string into UPPER case letters.

Syntax
UCase(String)
Example

Add a button and place the following function inside the same.

Private Sub Constant_demo_Click()


var = "Microsoft VBScript"
msgbox("Line 1 : " & UCase(var))
var = "MS VBSCRIPT"
msgbox("Line 2 : " & UCase(var))
var = "microsoft"
msgbox("Line 3 : " & UCase(var))
End Sub
VBA - Left
The Left function returns a specified number of characters from
the left side of the given input string.

Syntax
Left(String, Length)
Parameter Description
 String − A required parameter. Input String from which the
specified number of characters to be returned from the left
side.
 Length − A required parameter. An Integer, which specifies
the number of characters to be returned.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var as Variant
var = "Microsoft VBScript"
msgbox("Line 1 : " & Left(var,2))
var = "MS VBSCRIPT"
msgbox("Line 2 : " & Left(var,5))
var = "microsoft"
msgbox("Line 3 : " & Left(var,9))
End Sub
VBA - Right
The Right function returns a specified number of characters from
the right side of the given input string.

Syntax
Right(String, Length)
Parameter Description
 String − A required parameter. Input String from which the
specified number of characters to be returned from the right
side.
 Length − A required parameter. An Integer, which Specifies
the number of characters to be returned.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


var = "Microsoft VBScript"
msgbox("Line 1 : " & Right(var,2))
var = "MS VBSCRIPT"
msgbox("Line 2 : " & Right(var,5))
var = "microsoft"
msgbox("Line 3 : " & Right(var,9))
End Sub
VBA - Mid
The Mid Function returns a specified number of characters from a
given input string.

Syntax
Mid(String,start[,Length])
Parameter Description
 String − A required parameter. Input String from which the
specified number of characters to be returned.
 Start − A required parameter. An Integer, which specifies
the starting position of the string.
 Length − An optional parameter. An Integer, which
specifies the number of characters to be returned.

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var as Variant
var = "Microsoft VBScript"
msgbox("Line 1 : " & Mid(var,2))
msgbox("Line 2 : " & Mid(var,2,5))
msgbox("Line 3 : " & Mid(var,5,7))
End Sub
VBA - Ltrim
The Ltrim function removes the blank spaces from the left side of
the string.

Syntax
LTrim(String)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var as Variant
var = " Microsoft VBScript"
msgbox "After Ltrim : " & LTrim(var)
End Sub
VBA - Rtrim
The Rtrim function removes the blank spaces from the right side
of the string.

Syntax
RTrim(String)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var as Variant
var = "Microsoft VBScript "
msgbox("After Rtrim : " & RTrim(var))
End Sub
VBA - Trim
The Trim function removes both the leading and the trailing blank
spaces of the given input string.

Syntax
Trim(String)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


var = "Microsoft VBScript"
var = " Microsoft VBScript
"
msgbox ("After Trim : " & Trim(var))
End Sub
VBA - Len
The Len function returns the length of the given input string
including the blank spaces.

Syntax
Len(String)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var1 as Variant
Dim var2 as Variant
var1 ="Microsoft VBScript"
msgbox("Length of var1 is : " & Len(var1))
var2 = " Microsoft VBScript "
msgbox ("Length of var2 is : " & Len(var2))
End Sub
VBA - Replace
The Replace function replaces a specified part of a string with a
specific string, a specified number of times.

Syntax
Replace(string,find,replacewith[,start[,count[,compare]]])
Parameter Description
 String − A required parameter. The Input String which is to
be searched for replacing.
 Find − A required parameter. The part of the string that will
be replaced.
 Replacewith − A required parameter. The replacement
string, which would be replaced against the find parameter.
 Start − An optional parameter. Specifies the start position
from where the string has to be searched and replaced.
Default value is 1.
 Count − An optional parameter. Specifies the number of
times the replacement has to be performed.
 Compare − An optional parameter. Specifies the
comparison method to be used. Default value is 0.
o 0 = vbBinaryCompare - Performs a binary comparison
o 1 = vbTextCompare - Performs a Textual comparison
Example
Private Sub Constant_demo_Click()
Dim var as Variant
var = "This is VBScript Programming"
'VBScript to be replaced by MS VBScript
msgbox("Line 1: " & Replace(var,"VBScript","MS
VBScript"))
'VB to be replaced by vb
msgbox("Line 2: " & Replace(var,"VB","vb"))
''is' replaced by ##
msgbox("Line 3: " & Replace(var,"is","##"))
''is' replaced by ## ignores the characters before
the first occurence
msgbox("Line 4: " & Replace(var,"is","##",5))
''s' is replaced by ## for the next 2 occurences.
msgbox("Line 5: " & Replace(var,"s","##",1,2))
''r' is replaced by ## for all occurences textual
comparison.
msgbox("Line 6: " & Replace(var,"r","##",1,-1,1))
''t' is replaced by ## for all occurences Binary
comparison
msgbox("Line 7: " & Replace(var,"t","##",1,-1,0))
End Sub
VBA - Space
The Space function fills a string with a specific number of spaces.

Syntax
space(number)
Parameter Description
Number − A required parameter. The number of spaces that we
want to add to the given string.
Example
Private Sub Constant_demo_Click()
Dim var1 as Variant
var1 = "Microsoft"
Dim var2 as Variant
var2 = "VBScript"
msgbox(var1 & Space(2)& var2)
End Sub
VBA - strComp
The StrComp function returns an integer value after comparing
the two given strings. It can return any of the three values -1, 0,
or 1 based on the input strings to be compared.

 If String 1 < String 2, then StrComp returns -1


 If String 1 = String 2, then StrComp returns 0
 If String 1 > String 2, then StrComp returns 1
Syntax
StrComp(string1,string2[,compare])
Parameter Description
 String1 − A required parameter. The first string expression.
 String2 − A required parameter. The second string
expression.
 Compare − An optional parameter. Specifies the string
comparison to be used. It can take the following values.
o 0 = vbBinaryCompare - Performs Binary
Comparison(Default)
o 1 = vbTextCompare - Performs Text Comparison
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim var1 as Variant
msgbox("Line 1 :" &
StrComp("Microsoft","Microsoft"))
msgbox("Line 2 :" &StrComp("Microsoft","MICROSOFT"))
msgbox("Line 3 :" &StrComp("Microsoft","MiCrOsOfT"))
msgbox("Line 4 :"
&StrComp("Microsoft","MiCrOsOfT",1))
msgbox("Line 5 :"
&StrComp("Microsoft","MiCrOsOfT",0))
End Sub
VBA - String Function
The String function fills a string with the specified character for
specified number of times.

Syntax
String(number,character)
Parameter Description
 Number − A required parameter. An integer value, which
would be repeated for a specified number of times against
the character parameter.
 Character − A required parameter. Character value, which
has to be repeated for a specified number of times.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


msgbox("Line 1 :" & String(3,"$"))
msgbox("Line 2 :" & String(4,"*"))
msgbox("Line 3 :" & String(5,100))
msgbox("Line 4 :" & String(6,"ABCDE"))
End Sub
VBA - String Reverse Function
The StrReverse function reverses the specified string.

Syntax
StrReverse(string)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


msgbox("Line 1 : " & StrReverse("VBSCRIPT"))
msgbox("Line 2 : " & StrReverse("My First
VBScript"))
msgbox("Line 3 : " & StrReverse("123.45"))
End Sub

You might also like