REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

The StrReverse function is a built-in string function in VBA (Visual Basic for Applications) that can be used to reverse the order of characters in a string. It takes a string as its argument and returns a new string with the characters reversed. This function can be useful for a variety of applications, such as sorting names in reverse alphabetical order, checking for palindromes, or simply for display purposes.

VBA StrReverse Function – Purpose, Syntax and Arguments

Syntax

The syntax for the StrReverse function is:

StrReverse(string)

Where string is the string that you want to reverse.

Arguments

The only argument for this function is string, which is required and can be any valid string in VBA.

  • string: The string to be reversed.

Example

Let’s say we have a string variable named myString with the value “Hello World”. If we use the StrReverse function on this string, it will return a new string with the characters reversed, as shown in the code below:

Sub StrReverseExample()
    Dim myString As String
    myString = "Hello World"
    MsgBox StrReverse(myString)
End Sub

The output of this code will be “dlroW olleH”.

Remarks and Important Notes

  • The StrReverse function only works with single-byte character sets. This means that it may not produce the desired results for strings with double-byte characters, such as Chinese or Japanese characters.
  • If the string argument is an empty string, the function will return an empty string as well.
  • The StrReverse function is not case-sensitive, meaning it will reverse both uppercase and lowercase characters.
  • This function can also be used on numbers that have been converted to strings.
  • If you want to reverse the order of characters in a string, but don’t want to use the StrReverse function, you can use a loop to iterate through the characters and build a new string in reverse order.

The StrReverse function is a helpful tool for manipulating strings in VBA, making it easier to perform tasks that require the reversal of characters. It is a simple yet powerful function to use, and understanding its syntax, arguments, and important notes can help you utilize it effectively in your VBA projects.

Understanding VBA StrReverse Function with Examples

Example 1: Reverse a String

Description: The StrReverse function in VBA is used to reverse a given string. It takes a string as input and returns a string with the characters in reverse order.

Public Sub ReverseString()
    Dim str As String
    Dim reversedStr As String
    
    str = "Hello World!"
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: !dlroW olleH'
End Sub

Explanation: In the above code, we have defined a string variable named ‘str’ and assigned it the value “Hello World!”. Then, we have declared another string variable named ‘reversedStr’ which will store the reversed string.
The StrReverse function is then used on the ‘str’ variable and the result is assigned to the ‘reversedStr’ variable. Finally, a message box is displayed with the reversed string as the output.

Example 2: Reversing a Numeric String

Description: The StrReverse function is not limited to reversing only alphabetic strings. It can also reverse numeric strings as shown in the following example.

Public Sub ReverseNumericString()
    Dim num As String
    Dim reversedNum As String
    
    num = "12345"
    
    reversedNum = StrReverse(num)
    
    MsgBox reversedNum   'Output: 54321'
End Sub

Explanation: Similar to the previous example, a string variable named ‘num’ is defined and assigned the value “12345”. The StrReverse function is then used on the ‘num’ variable and the result is assigned to the ‘reversedNum’ variable. The message box displays the reversed numeric string as the output.

Example 3: Reversing a String with Multiple Words

Description: The StrReverse function can also be used to reverse a string with multiple words, as shown in the following example.

Public Sub ReverseMultipleWords()
    Dim str As String
    Dim reversedStr As String
    
    str = "Hello World!"
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: !dlroW olleH'
End Sub

Explanation: In this example, the string variable ‘str’ is assigned the value “Hello World!”. The StrReverse function is then used on this string, resulting in the words being reversed but not the individual letters within each word. This is because the StrReverse function operates on the entire string as one entity, rather than each word separately.

Example 4: Reversing Special Characters in a String

Description: The StrReverse function can also reverse special characters in a string, such as punctuation marks, as shown in the following example.

Public Sub ReverseSpecialCharacters()
    Dim str As String
    Dim reversedStr As String
    
    str = "Hello! How are you?"
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: ?uoy era woH !olleH'
End Sub

Explanation: In this example, the string variable ‘str’ is assigned the value “Hello! How are you?”. The StrReverse function is used on this string, resulting in the special characters being reversed along with the alphabetic characters. This demonstrates that the function treats all characters equally and does not differentiate between alphabets and special characters.

Example 5: Reversing an Empty String

Description: It is also possible to reverse an empty string using the StrReverse function, as shown in the following example.

Public Sub ReverseEmptyString()
    Dim str As String
    Dim reversedStr As String
    
    str = ""
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: Empty String'
End Sub

Explanation: In this example, the string variable ‘str’ is assigned an empty string. The StrReverse function is used on this string, resulting in the variable ‘reversedStr’ also being assigned an empty string. This demonstrates that the function can be used on any type of string, including empty ones.

Example 6: Reversing a String with Spaces

Description: The StrReverse function does not ignore spaces when reversing a string, as shown in the following example.

Public Sub ReverseStringWithSpaces()
    Dim str As String
    Dim reversedStr As String
    
    str = "Hello World!"
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: !dlroW olleH '
End Sub

Explanation: In this example, the string variable ‘str’ is assigned the value “Hello World!”, which includes a space between the words. The StrReverse function is then used on this string, resulting in the space also being reversed along with the characters. This shows that the function does not ignore any characters, including spaces.

Example 7: Reversing a String with Non-English Characters

Description: The StrReverse function can also be used on strings with non-English characters, as shown in the following example.

Public Sub ReverseNonEnglishString()
    Dim str As String
    Dim reversedStr As String
    
    str = "Привет мир!"
    
    reversedStr = StrReverse(str)
    
    MsgBox reversedStr   'Output: !рим тевирП'
End Sub

Explanation: In this example, the string variable ‘str’ is assigned the value “Привет мир!”, which is the Russian equivalent of “Hello World!”. The StrReverse function is used on this string, resulting in the Russian characters being reversed as well. This demonstrates the versatility of the function and its ability to handle different types of characters.

Conclusion:

The StrReverse function in VBA is a useful tool for reversing strings. It can be used on different types of strings, including alphabetic, numeric, strings with special characters, and even empty strings. This function provides a simple and efficient way to reverse strings in any VBA program.

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA FunctionsTags: , , , Last Updated: September 30, 2023

Leave A Comment