VBA - Strings


Advertisements


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.

S.No. Function Name & Description
1 InStr

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

2 InstrRev

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

3 Lcase

Returns the lower case of the specified string.

4 Ucase

Returns the upper case of the specified string.

5 Left

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

6 Right

Returns a specific number of characters from the right side of the string.

7 Mid

Returns a specific number of characters from a string based on the specified parameters.

8 Ltrim

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

9 Rtrim

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

10 Trim

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

11 Len

Returns the length of the given string.

12 Replace

Returns a string after replacing a string with another string.

13 Space

Fills a string with the specified number of spaces.

14 StrComp

Returns an integer value after comparing the two specified strings.

15 String

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

16 StrReverse

Returns a string after reversing the sequence of the characters of the given string.



Advertisements