Search
Close this search box.

VBA – StrReverse String


The StrComp function in VBA is used to compare two strings and determine their relationship based on a specified comparison method. It returns an integer value that indicates whether the first string is less than, equal to, or greater than the second string.

Syntax:

StrReverse(string)

Parameter:

 

  • string1: The first string to be compared.
  • string2: The second string to be compared.
  • compare: (Optional) An integer value that specifies the comparison method:
    • 0 or vbBinary: Performs a binary comparison (case-sensitive).
    • 1 or vbTextCompare: Performs a text comparison (case-insensitive).
    • vbDatabaseCompare: Uses the database’s collation settings for comparison.

Return Values:

  • -1: If string1 is less than string2.
  • 0: If string1 is equal to string2.
  • 1: If string1 is greater than string2.

 

Case-Insensitive Comparison

				
					Dim result As Integer
result = StrComp("apple", "Apple") 
' result will be -1 (because "apple" comes before "Apple" in binary comparison)
				
			

Case-Insensitive Comparison

				
					Dim result As Integer
result = StrComp("apple", "Apple", vbTextCompare) 
' result will be 0 (because "apple" and "Apple" are considered equal in text comparison)
				
			

Comparing Numbers as Strings:

				
					Dim result As Integer
result = StrComp("10", "2") 
' result will be 1 (because "10" comes after "2" when compared as strings)
				
			

Use Cases:

  • Sorting: Sorting arrays or lists of strings alphabetically or numerically.
  • Data Validation: Checking if two strings are identical or if one string precedes or follows another in a specific order.
  • Conditional Logic: Making decisions in your VBA code based on the comparison of strings.

 

Key Considerations:

  • The compare argument is optional. If omitted, vbBinary (case-sensitive) is used by default.
  • The StrComp function is a powerful tool for comparing strings in various scenarios, providing flexibility in how you want to compare them.