Comparing
When you are comparing 2 strings you are actually comparing the ANSI value of each character to the value of the corresponding character in the other string.
To match the special characters, left bracket ([), question mark (?), number sign {#) and asterisk (*), enclose them in brackets.
The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.
A quick way to return the character in a particular position in a string is to use the MID(sText,iCharNo,1)
However this can be changed by using the Option Compare statement.
To perform a string comparison within a procedure and override the string comparison setting for the module, you can use the StrComp function.
Empty Strings
To check if a string variable contains any data you have a couple of choices.
Method 1 - You can compare it to a zero-length string.
Using the empty string "" will allocate a variable length string and use more resources.
Dim sMyString As String
If (sMyString = "") Then
End If
Method 2 - You can use the Len() function and check if it is zero.
If (Len(sMyString) = 0) Then
End If
Method 3 - You can compare it to the built-in constant vbNullString.
If (sMyString = vbNullString) Then
End If
Comparison Operators
Character strings (like numbers) can be compared with the following numerical operators (=, <, >) and combinations of these.
Strings can be easily compared using any of the following operators.
When you use these operators the results depend on the string comparison setting for the module.
String comparisons are done by comparing each character in the string.
If one string matches all the characters on the left side of the other string the shorter string is always less than the longer string
| < | less than |
| <= | less than or equal to |
| = | equal to |
| <> | not equal to |
| >= | greater than or equal to |
| > | greater than |
| LIKE | The LIKE operator can also be used |
A < B < a < b
"3 A" < "3 C" < "3- B"
"russell" < "russellproctor"
Functions with Compare
The following is a list of functions that have an optional compare/vbCompareMethod argument.
| Function | Description |
| STRCOMP | Returns the result of a string comparison (Integer). |
| INSTR | Returns the position of a substring within a larger string (Long). |
| INSTRREV | Returns the position of a substring within a larger string, starting at the end (Long). |
| REPLACE | Returns the text string with a number of characters replaced (String). |
| FILTER | Returns an array containing a subset of values that contain a substring (Variant). |
| SPLIT | Returns an array containing a specified number of substrings (Variant). |
VBA.vbStrConv
| vbUppercase | 1 | Converts a string to all uppercase characters |
| vbLowercase | 2 | Converts a string to all owercase characters |
| vbPropercase | 3 | Converts the first letter of every word in a string to uppercase |
vbCompareMethod
STRCOMP
Option Compare
Returns the result of a string comparison (Integer).
If you omit the optional compare argument then the module's default comparison setting will be used.
A much safer way to compare strings rather than relying on module level statements and comparison operators is to use this function.
The STRCOMP function will override any module level string comparison settings.
However if you do no explicitly define the VBA.vbCompareMethod in this function it will use the compare method defined at the top of the module.
| VBA.vbCompareMethod | |
| vbBinaryCompare | Default Case Sensitive - based on a sort order derived from their internal binary representation. Performs a binary string comparison (A < a) |
| vbTextCompare | Case-Insensitive - based on order in the ASCII table Performs a textual string comparison (A = a) |
| vbDatabaseCompare | Case-Insensitive - This can only be used within Microsoft Access. Performs a comparison based on information contained within a Microsoft Access database |
If a module does not include an Option Compare statement at the top then Binary comparison is used.
Option Compare Binary - Case Sensitive
Option Compare Binary
This means that the individual characters are compared meaning that uppercase characters are always less than lowercase characters.
A < B < a < b
This compares strings based on a sort order derived from their internal binary representation of each character.
StrComp(string1, string2 [,compare])
StrComp("vba", "VBA", vbBinaryCompare) = 1
StrComp("vba", "VBA", vbTextCompare) = 0
StrComp("A", "B") = -1
StrComp("A", "A") = 0
StrComp("B", "A") = 1
StrComp("A", "B", vbBinaryCompare) = -1
StrComp("A", "A", vbBinaryCompare) = 0
StrComp("B", "A", vbBinaryCompare) = 1
StrComp("a", "B") = -1
StrComp("a", "A") = 0
StrComp("b", "A") = 1
StrComp("a", "B", vbBinaryCompare) = -1
StrComp("a", "A", vbBinaryCompare) = 0
StrComp("b", "A", vbBinaryCompare) = 1
StrComp("5", "5", vbBinaryCompare) = 0
StrComp("10", "5", vbBinaryCompare) = -1
StrComp("5", "10", vbBinaryCompare) = 1
StrComp("TEXT", "text", vbBinaryCompare) = -1
StrComp("3 A", "3- B", vbBinaryCompare) = -1
StrComp("3 C", "3- B", vbBinaryCompare) = -1
Option Compare Text - Not Case Sensitive
Option Compare Text
This compares strings based on case-insensitive text sort order
Never use this
These two text strings are identical but are in different cases
StrComp("TEXT", "text", vbTextCompare) = 0
"3 A" < "3- B" < "3 C"
StrComp("3 A", "3- B", vbTextCompare) = -1
StrComp("3 C", "3- B", vbTextCompare) = 1
Option Compare Database
Only available for Access databases. For new modules this is added automatically.
Option Compare Database
This can only be used within Microsoft Access.
The default is case-insensitive.
This compares strings based on the sort order determined by the local ID of the database whether the string comparison occurs.
This is the default string comparison setting but if this is removed from the top of the module, then Option Compare Binary will be used.
Option Base 1
Like Operator
The Like Operator can be used to compare two strings and provide pattern matching.
The behaviour of the Like operator depends on the Option Compare statement.
The default string comparison method is Option Compare Binary which means that string comparisons are based on a sort order derived from the binary representations of the characters meaning A < a.
If (sFirstName Like "a?b*" = True) Then
End If
You can add "Option Compare Text" to the top of the module to force string comparisons to be based on case insensitive text. So "A" is the same as "a".
You can eliminate the case sensitivity when comparing strings by declaring Option Compare Text (ie A = a). Be aware that some fonts are not monospaced (eg Courier New), which means that characters are not equally spaced.
When comparing individual characters it is better to compare ASCII numbers using ASC(char) =- 137 rather than char = "%".
Pattern Characters [ result = String LIKE pattern ]
| Pattern Character | What it matches in a string |
| ? | Matches a single character |
| * | Matches zero or more characters |
| # | Matches a single digit (0-9) |
| [charlist] | Matches a single character in charlist |
| [!charlist] | Matches a single character not in the charlist |
You can use a hyphen to separate the lower and upper bounds, eg [A-Z]. There are no delimiters.
Special Characters
To match the special characters, left bracket ([), question mark (?), number sign {#) and asterisk (*), enclose them in brackets.
The right bracket (]) cannot be used within a group to match itself, but it can be used outside a group as an individual character.
A quick way to return the character in a particular position in a string is to use the MID(sText,iCharNo,1)
| Pattern Characters | |
| [ABC] | "A" or "B" or "C" |
| [A - D] | "A" or "B" or "C" or "D" |
| [A - Z] | Any uppercase alphabetical character |
| [A - Za - z] | Any uppercase or lowercase alphabetical character |
"ABC" Like "A?C" = True
"A4C Like "A#C" = True
"ABC" Like "A??C" = False
"A123C" Like "A*C" = True
"1B3" Like "1[A-Z]3" = True
"123" Like "1[!A-Z]3" = True
"0207 457 1234" Like "#### ### ####" = True
Wildcard Characters
In VBA this can be done by placing these characters inside square brackets [ ].
Debug.Print "a*c" Like "a[*]c" '= True
Debug.Print "a?c" Like "a[?]c" '= True
Debug.Print "abc" Like "a[*]c" '= False
Debug.Print "abc" Like "a[?]c" '= False
Debug.Print "abc" Like "a*c" '= True
Debug.Print "abc" Like "a?c" '= True
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext