0% found this document useful (0 votes)
177 views2 pages

VB A String Cheat Sheet

This document provides a cheat sheet on common string operations and functions in VBA. It includes examples of how to assign, concatenate, trim, replace and convert strings. It also shows how to find characters or words within strings, split strings into arrays, and join arrays into strings.

Uploaded by

Naren Burra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views2 pages

VB A String Cheat Sheet

This document provides a cheat sheet on common string operations and functions in VBA. It includes examples of how to assign, concatenate, trim, replace and convert strings. It also shows how to find characters or words within strings, split strings into arrays, and join arrays into strings.

Uploaded by

Naren Burra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

VBA String Cheat Sheet

VBA String Data Types Reminder Convert String to Lowercase


String Green Text
Dim myStr as String
Used to hold text myStr = “I LOVE PIZZA”
Used to hold comments myStr = Lcase(myStr)
Variant ‘This is a comment ‘myStr now contains “i love pizza”
Can hold text, but it is better to declare your variable as a Bold Code
string. The code in bold is the focus of the section. For example,
in the string split section the Split keyword is in bold
Finding Characters or Words in VBA Strings
Using Sub
Basic VBA String Operations Find Character Position In String
You can copy/paste these examples into a VBA editor. Just Dim myStr as String, strLocation as
Assign Text to String Variable
make sure you put them into a Sub Integer
Dim favFood As String
favFood = “Pizza” myStr = “I love pizza”
‘Strings must be contained within strLocation = InStr(myStr, “z”)
‘parenthesis VBA Operations to Change String Variables ‘strLocation now contains the value 10
Concatenate String With String Left Slice of String Find Word Position In StringFrom the Left
Dim fullName as String Dim fullName as String, firstName as Dim myStr as String, strLocation as
fullName = “Joe “ & “Brown” String Integer
fullName = “George Washington” myStr = “I love pizza”
Concatenate String With Variable
firstName = Left(fullName, 6) strLocation = InStr(myStr, “pizza”)
Dim fullName as String, firstName as
‘firstName now contains “George” ‘strLocation now contains the value 8
String
firstName = “Joe” Right Slice of String
fullName = firstName & “ Brown” Dim fullName as String, lastName as
Concatenate Variable With Variable String Splitting Strings
Dim fullName as String, firstName as fullName = “George Washington” Split sentence into array containing invidual words
String, lastName as String lastName = Right(fullName, 10) Dim myStr As String, strArray() As
firstName = “Joe” ‘firstName now contains “Washington” String
lastName = ”Brown” Middle Slice of String myStr = "I love pizza"
fullName = firstName & “ “ & lastName Dim fullName as String, lastName as strArray = Split(myStr)
String 'strArray now contains a one dimensional
Get Length Of String
fullName = “George Ed Washington” ‘array containing “I”, “love” and
Dim fullName as String, stringLength as
lastName = Mid(fullName, 8,2) ‘“pizza”
Integer
‘firstName now contains “Ed” Split comma delimited string into array of individual words
fullName = “Joe Brown”
stringLength = Len(fullName) Replace Character in String Dim myStr As String, strArray() As
‘stringLength now contains the value 9 Dim myStr as String String
myStr = “joy” myStr = "john,smith,large,onion"
Trim String
myStr = Replace(myStr, “j”, “t”) strArray = Split(myStr, “,”)
Dim fullName As String, stringLength as ‘myStrnow contains “toy”
Integer 'strArray now contains a one dimensional
Replace Word in String ‘array containing “john”, “smith” and
fullName = “ George Washington “
Dim myStr as String ‘“large” and “onion”
stringLength = Len(fullName)
myStr = “I love pizza”
‘stringLength now contains the value 21
myStr = Replace(myStr, “pizza”, “fruit”)
fullName= Trim(fullName)
‘myStrnow contains “I love fruit”
stringLength = Len(fullName) Joining Strings
‘stringLength now contains the value 17 Join an array of strings into a single string
Dim myStr As String, strArray(0 to 2) As
Convert Value to String VBA String Case Operators String
Dim myInt as Integer, myStr as String Convert String to Uppercase strArray(0) = "I"
myInt = 100 Dim myStr as String strArray(1) = "love"
myStr = CStr(myInt) myStr = “i love pizza” strArray(2) = "pizza"
‘myStr contains “100” and myInt contains myStr = Ucase(myStr) myStr = Join(strArray)
100 ‘myStr now contains “I LOVE PIZZA” 'myStr now contains “I love pizza”

© 2020, Code With VBA, All Rights Reserved


VBA String Cheat Sheet
Join an Array of Strings with comma delimiter
Dim myStr As String, strArray(0 to 2) As
String
strArray(0) = "I"
strArray(1) = "love"
strArray(2) = "pizza"
myStr = Join(strArray, “,”)
'myStr now contains “I,love,pizza”

© 2020, Code With VBA, All Rights Reserved

You might also like