JIYIK CN >

Current Location:Home > Learning > PROGRAM > Vba >

Comparing Strings in VBA

Author:JIYIK Last Updated:2025/04/16 Views:

We will look at how to compare strings in VBA using different methods.

Comparing Strings in VBA

While working with excel worksheets, there are situations where we have to compare strings and want to get the result of which string is less than or greater than the other string. VBA provides a perfect method for this called StrComp(), which compares two strings to get a numerical result.

This method will return if the two strings we compare are equal 0. If the first string is less than the other, we will get -1as the return value.

If the first input string is greater than the other, we will get value 1as the result. If one of the user input string is empty, then the return value will be Null.

StrComp()The String comparison method uses three comparison methods, but only two of them are most commonly used, and we will discuss these two methods. If we want to compare two strings in binary, we can use vbBinaryCompareString comparison, which is case-sensitive.

If we want to compare the text of two strings, we can use vbTextCompare, a case insensitive method. Let's take an example and compare two strings.

Code:

# VBA
Sub test()
Dim compare As Integer
stringOne = "Hello World"
stringTwo = "Hello World"

compare = StrComp(stringOne, stringTwo)

MsgBox compare

End Sub

Output:

Comparing Strings in VBA Using the StrComp Method

In the above example, we passed the same string that we got 0as the return value. Let's pass different values ​​and check what return value we get.

Code:

# VBA
Sub test()
Dim compare As Integer
stringOne = "Hello World"
stringTwo = "Hello World!"

compare = StrComp(stringOne, stringTwo)

MsgBox compare

End Sub

Output:

Use the StrComp method to compare strings in VBA and provide different values

From the above code, we are passing different strings we get -1the length of as return value since the second string is greater than the first. Let us try StrComp()the string comparison method in and check how the result is different if we use the same string but with different case and use vbTextComparethe method.

Code:

# VBA
Sub test()
Dim compare As Integer
stringOne = "Hello World"
stringTwo = "Hello WorLd"

compare = StrComp(stringOne, stringTwo, vbTextCompare)

MsgBox compare

End Sub

Output:

Comparing Strings in VBA Using the StrComp Method and Using vbTextCompare

Now, we use the method in our example by adding an extra space vbBinaryCompare.

Code:

# VBA
Sub test()
Dim compare As Integer
stringOne = "Hello World"
stringTwo = "Hello  World"

compare = StrComp(stringOne, stringTwo, vbBinaryCompare)

MsgBox compare

End Sub

Output:

Comparing Strings in VBA Using the StrComp Method and Using vbBinaryCompare

This way, we can use StrComp()the method and built-in methods such as vbTextCompareand vbBinaryCompareto compare two strings based on length or text.

Previous: None

Next:Concatenating strings in VBA

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Remove duplicates in VBA

Publish Date:2025/04/16 Views:104 Category:Vba

We will explain how to remove duplicates in VBA by example. Remove duplicates in VBA When working with an Excel worksheet that contains a lot of data, it is likely that this data contains some duplicates. Any duplicates must be removed to a

Rounding in VBA

Publish Date:2025/04/16 Views:162 Category:Vba

Round() We will introduce the and functions in VBA through examples RoundUp() . Round() Using the or RoundUp() function in VBA When working with Excel worksheets containing numbers and calculations, we get results that are fractions. Someti

Adding a new row in VBA

Publish Date:2025/04/16 Views:194 Category:Vba

We will show you how to continue the code on the next line in VBA by example. We will also show you how to go to the next line in a message box using different methods in VBA. Adding a new row in VBA There are two different situations in pr

Sum function in VBA

Publish Date:2025/04/16 Views:126 Category:Vba

We will show you how to use sum in VBA. Sum function in VBA Sum is the most commonly used function in excel. This function is very useful as we can use sum to get the total from the financial table. We will learn how to use the sum function

Exit Sub in VBA

Publish Date:2025/04/16 Views:191 Category:Vba

We will look at different ways to exit a sub in VBA through examples. Using the Exit Sub Statement in VBA While using Sub in VBA, we may want to exit it or prevent it from executing further if an error occurs or the user provides wrong inpu

Calling a Sub in VBA

Publish Date:2025/04/16 Views:190 Category:Vba

We will show you how to call a sub within another sub through examples in VBA. Calling a Sub in VBA When dealing with multiple subroutines, we may encounter situations where we need to call multiple subroutines for the same function. Some f

Check if a string contains a substring in VBA

Publish Date:2025/04/16 Views:189 Category:Vba

This article demonstrates the use of Instr() the , , InstrRev() and Like functions to check whether a main string contains a substring. Use Instr() the function to check if the main string contains a substring Instr() Function syntax: InStr

Convert a string to a number in VBA

Publish Date:2025/04/16 Views:188 Category:Vba

This article will discuss how to convert a string to a number in VBA Excel. There is a step-by-step guide and many examples for us to understand. Convert a string to a number in VBA In VBA code, numbers saved as text must be converted to re

Concatenating strings in VBA

Publish Date:2025/04/16 Views:112 Category:Vba

We will cover how to concatenate strings in VBA and explain how to use Excel and operators. Concatenating strings in VBA We can concatenate multiple strings into a singular string in VBA and use the operator to separate the values ​​of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial