Learn essential text processing techniques for Excel automation
The InStr function searches for a substring within a larger string and returns its position. This is particularly useful for data validation or parsing complex strings.
Private Sub cmdInstr_Click() Dim phrase As String phrase = Cells(1, 1).Value Cells(4, 1) = InStr(phrase, "ual") End Sub
This code searches for "ual" in the text from cell A1. For "Visual Basic", it returns 4 (the starting position of "ual").
Extract a specified number of characters from the start of a string.
Private Sub cmdLeft_Click() Dim phrase As String phrase = Cells(1, 1).Value Cells(2, 1) = Left(phrase, 4) End Sub
Extracts first 4 characters ("Visu") from "Visual Basic".
Private Sub ExtractAreaCode() Dim phoneNumber As String phoneNumber = "(123) 456-7890" Cells(1, 2) = Mid(phoneNumber, 2, 3) 'Extracts "123" End Sub
Get characters from the end of a string - useful for file extensions or suffixes.
Private Sub cmdRight_Click() Dim phrase As String phrase = Cells(1, 1).Value Cells(3, 1) = Right(phrase, 5) End Sub
Extracts last 5 characters ("Basic") from "Visual Basic".
Private Sub GetFileExtension() Dim fileName As String fileName = "report_2023.xlsx" Cells(1, 2) = Right(fileName, 5) 'Returns ".xlsx" End Sub
The Mid function is versatile for extracting substrings from any position.
Private Sub cmdMid_Click() Dim phrase As String phrase = Cells(1, 1).Value Cells(5, 1) = Mid(phrase, 8, 3) End Sub
Extracts "Bas" starting from position 8 (3 characters long).
Private Sub ExtractMiddleName() Dim fullName As String, space1 As Integer, space2 As Integer fullName = "John Quincy Adams" space1 = InStr(fullName, " ") space2 = InStr(space1 + 1, fullName, " ") If space2 > 0 Then Cells(1, 2) = Mid(fullName, space1 + 1, space2 - space1 - 1) End If End Sub
Determine the number of characters in a string, including spaces.
Private Sub cmdLen_Click() Dim phrase As String phrase = Cells(1, 1).Value Cells(6, 1) = Len(phrase) End Sub
Returns 12 for "Visual Basic" (including the space).
Standardize text case for comparisons or formatting.
Private Sub StandardizeCase() Dim userInput As String userInput = "Excel VBA" Cells(1, 2) = UCase(userInput) 'EXCEL VBA Cells(2, 2) = LCase(userInput) 'excel vba End Sub
If UCase(input) = "YES" Then
Convert between numeric and string representations.
Private Sub ConvertTypes() Dim num As Double, text As String num = 123.45 text = "456.78" Cells(1, 2) = Str(num) 'Converts to " 123.45" (note leading space) Cells(2, 2) = Val(text) 'Converts to 456.78 End Sub
Work with ASCII codes for special characters or control codes.
Private Sub ASCIIExamples() 'Common ASCII codes Cells(1, 1) = "A = " & Asc("A") '65 Cells(2, 1) = "a = " & Asc("a") '97 Cells(3, 1) = "Tab = " & Asc(vbTab) '9 'Special characters Cells(4, 1) = Chr(169) '© Cells(5, 1) = Chr(174) '® End Sub
Real-world applications often combine multiple string functions.
Private Sub ExtractEmailDomain() Dim email As String, atPos As Integer email = "[email protected]" atPos = InStr(email, "@") If atPos > 0 Then Cells(1, 2) = Mid(email, atPos + 1) End If End Sub
Private Sub CleanData() Dim rawInput As String, cleanOutput As String rawInput = " Mixed CASE input 123 " 'Trim spaces, proper case first letter, lowercase rest cleanOutput = UCase(Left(Trim(rawInput), 1)) & _ LCase(Mid(Trim(rawInput), 2)) Cells(1, 2) = cleanOutput 'Returns "Mixed case input 123" End Sub
For complex string operations, consider using Regular Expressions via the VBScript RegExp object for pattern matching and advanced text manipulation.
Copyright ® 2023 Dr. Liew Voon Kiong . All rights reserved [Privacy Policy]
Contact: Facebook Page