VBA string function
VBA string function
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
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
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
Syntax
Lcase(String)
Example
Add a button and place the following function inside the same.
Syntax
UCase(String)
Example
Add a button and place the following function inside the same.
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
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
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.
Syntax
LTrim(String)
Example
Syntax
RTrim(String)
Example
Syntax
Trim(String)
Example
Syntax
Len(String)
Example
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.
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
Syntax
StrReverse(string)
Example