String Based Functions

Someone famous once asked, "What's in a name?" Someone less famous (yours truly) once asked, "What's in a string?" So what is in a string? Well, lots. Strings are key building blocks in any high-level programming language. More specifically, they are data structures that contain one or more characters. Note that it is also possible for strings to be Null (undefined).

Groupings of characters and numbers comprise strings. These groupings of characters can mean different things depending on their use. Many languages, including VBA, provide popular means for parsing, searching, and managing the individual pieces (characters and numbers) that make up strings.

In this section I show you how to parse, search, and manage strings using some very popular built-in VBA functions, which are described in Table 5.1.

Table 5.1 Common String-Based Functions

Function Name Description

UCase Converts a string to uppercase

LCase Converts a string to lowercase

Len Returns the number of characters in a string

StrCom Compares two strings and determines if they are equal to, less than, or greater than each other

Right Determines the specified number of characters from the right side of a string

Left Determines the specified number of characters from the left side of a string

Mid Determines the specified number of characters in a string

InStr Finds the first occurrence of a string within another

Format Formats a string based on specified instructions


The UCase function is an easy function to use. It takes a string as a parameter and returns the string in uppercase letters.

Private Sub cmdConvert_Click()

txtOutput.Value = UCase(txtInput.Value) End Sub

The UCase function can take a string literal ("This is a string literal"), string variable (Dim sFirstName As String), or string type property (txtFirstName.Value) and provide output like that in Figure 5.1.


The inverse of UCase, the LCase function takes a string parameter and outputs the string in lowercase. Sample code is demonstrated next, with output seen in Figure 5.2.

Converting a string to uppercase using the UCase function.

Private Sub cmdConvert_Click()

txtOutput.Value = LCase(txtInput.Value) End Sub

Converting a string to lowercase using the LCase function.

Converting a string to uppercase using the UCase function.

m Eile Edit View Insert Format Records Tools Window Help _ รถ1 x


Output: vba programming

Convert to Lower : Case

0 0


  • michelle
    What is a stringbased?
    7 years ago

Post a comment