Search
Close this search box.

VBA – StrReverse String


The StrReverse function in VBA is used to reverse the order of characters within a given string.

Syntax:

StrReverse(string)

Parameter:

 

  • string: The string whose characters you want to reverse.


Return Value:

  • String: Returns the original string with the order of its characters reversed.

 

Example

				
					Dim str1 As String
Dim revStr As String

str1 = "Hello, World!"
revStr = StrReverse(str1) 

' revStr will be "!dlroW ,olleH"
				
			

Use Cases:

  • String Manipulation:
    • Reversing the order of words or characters for specific purposes.
    • Creating unique identifiers or codes.
    • Data encryption (though basic and not very secure).
  • Algorithmic Puzzles:
    • Solving string-related puzzles and challenges.
    • Implementing certain data structures (e.g., stacks).

Example: (Palindrome Check))

				
					Sub IsPalindrome()
  Dim str1 As String
  Dim revStr As String

  str1 = InputBox("Enter a word:")
  revStr = StrReverse(str1)

  If str1 = revStr Then
    MsgBox "The word is a palindrome!"
  Else
    MsgBox "The word is not a palindrome."
  End If
End Sub