To reverse text in Excel VBA, use the built-in StrReverse function: StrReverse("Excel") returns "lecxE". For a reusable worksheet formula, put a small VBA function in a standard module and call it from a cell; for a one-time change, run a macro on selected text cells.
What StrReverse Does
StrReverse is a Visual Basic for Applications function that returns a string with its characters in reverse order. It reverses the sequence of characters; it does not reverse the words as separate units, sort the text, or change letter case. For example, StrReverse("North 42") returns "24 htroN", including the two spaces in reverse positions.
The basic syntax is StrReverse(expression), where expression is the text or value to reverse. The result is a string. A zero-length string stays empty, and a Null expression returns Null. In typical Excel work, the input will be a cell value or a string variable.
This is a VBA feature, not a normal worksheet function. Typing =StrReverse(A1) into a cell will not work as a built-in Excel formula. You need either to call it from VBA code or create a VBA user-defined function (UDF) that Excel can use in a formula.
Create a Reusable Worksheet Function
A UDF is useful when you want a reversed result beside each original value and want that result to update when the source text changes. In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Developer > Visual Basic or press Alt+F11. On Mac, use Developer > Visual Basic; the keyboard shortcut can depend on the keyboard and may require the Fn key. In the editor, choose Insert > Module, then add this code:
Option Explicit
Public Function ReverseText(ByVal text As String) As String
' Return the input characters in reverse order.
ReverseText = StrReverse(text)
End Function
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) so it can retain VBA code. If the code is stored in a standard module in that workbook, enter =ReverseText(A2) in a worksheet cell to reverse the text in A2. For example, if A2 contains Invoice-572, the formula returns 275-eciovnI. Fill the formula down to apply it to other rows.
The function returns a result without changing the source cell. That makes it safer for lists where you need to preserve the original values, and it lets you compare the original and reversed strings side by side. If Excel displays #NAME?, check that the function is in a standard module, that the workbook is saved as .xlsm or another macro-capable format, and that macros are enabled under your organization’s security rules.
Reverse Selected Cells with a Macro
If you need to replace text in place rather than create a second column, use a macro on a deliberately selected range. This example reverses text constants only. It skips cells containing formulas, numbers, and error values, so it is less likely to overwrite something unintentionally:
Option Explicit
Sub ReverseSelectedText()
Dim cell As Range
' Work only with a selected worksheet range.
If TypeName(Selection) <> "Range" Then Exit Sub
For Each cell In Selection.Cells
' Preserve formulas and non-text values.
If Not cell.HasFormula Then
If Not IsError(cell.Value2) Then
If VarType(cell.Value2) = vbString Then
cell.Value2 = StrReverse(CStr(cell.Value2))
End If
End If
End If
Next cell
End Sub
Before running it, save a copy of the workbook. Select only the cells you intend to change, then run ReverseSelectedText from the Macro dialog or the Visual Basic Editor. The macro changes the selected text values directly. Excel does not provide a normal Ctrl+Z undo for VBA macro changes, so the copied workbook is an important safeguard. On Mac, the same macro can be run through Excel’s macro tools or the Visual Basic Editor.
This example intentionally leaves formulas unchanged, including formulas whose displayed result is text. It also skips numeric cells rather than converting numbers to text and reversing their digits. If your task calls for those behaviors, adjust the conditions only after deciding how dates, leading zeros, formulas, and other special values should be handled.
How Character Reversal Works
Reversal changes the order of characters, not their meaning. A string such as AB-19 becomes 91-BA; the hyphen moves because it is one of the characters in the string. A space, punctuation mark, or digit is treated just like a letter. This is often exactly right for a simple identifier transformation, but it is not the same as reversing a list of words.
For instance, reversing red blue produces eulb der, not blue red. To reverse word order while keeping each word spelled normally, you would need separate logic to split the text into words and rebuild them in the opposite order. Similarly, reversing a string is not a secure way to obscure or encrypt information; anyone can reverse it back.
Many ordinary names, codes, and English words work as expected with StrReverse. Some Unicode text is more complicated. A visible symbol can be made from multiple code points, such as a base letter plus a combining accent, or from a pair of UTF-16 code units, as with many emoji. A simple character-reversal routine may separate parts that a reader expects to stay together. If your data includes emoji, combining marks, or scripts that use complex shaping, test representative examples and use a grapheme-aware solution if visual integrity is essential.
Handling Blanks, Errors, and Other Cell Values
Decide how the code should treat non-text inputs before applying it to a large range. The UDF shown accepts a String argument, while the selected-cell macro explicitly processes only cells whose underlying value is text. These choices avoid silently turning every date or number into a different-looking string.
- Blank cells: An empty string remains empty. The macro also skips a truly empty cell because its value is not a string.
- Numbers and dates: The sample macro skips them. Converting them first can produce text whose appearance depends on formatting or the way the value is converted.
- Error values: The macro checks
IsErrorbefore attempting to reverse a cell, so an Excel error is not passed toStrReverse. - Formula cells: The macro skips them to avoid replacing formulas with fixed values. A UDF formula, by contrast, leaves its source cell intact.
If you modify the macro to accept more types, consider how it should handle leading zeros in product codes, dates displayed in a particular format, and cells with formulas. Those details can matter more than the reversal operation itself.
VBA Compared with a Worksheet Formula
For a task that must remain formula-only, Microsoft 365 and Excel 2021 also support dynamic-array functions that can build a reversed string. With the source text in A2, this formula reverses its characters and returns a blank when A2 is blank:
=IF(A2="","",TEXTJOIN("",TRUE,MID(A2,SEQUENCE(LEN(A2),,LEN(A2),-1),1)))
If A2 contains Excel, the result is lecxE. This approach uses SEQUENCE to generate character positions from the string’s length down to 1, MID to extract each character, and TEXTJOIN to join the extracted characters. It avoids VBA, but it is less compact than StrReverse and requires a version with the listed functions.
Choose VBA when the workbook already uses macros, when you want a short UDF, or when a macro must change selected cells. Choose a worksheet formula when macro-free sharing and ordinary formula auditing matter more. Excel for the web can calculate supported worksheet formulas, but it cannot run VBA macros; use desktop Excel to create or execute the VBA solution.
Common Problems and Safe Use
A frequent mistake is putting VBA code in the wrong place. A UDF intended for worksheet use normally belongs in a standard module, not in a worksheet cell and not in a cell’s formula bar as raw VBA. Another common issue is saving as .xlsx: that format does not retain VBA code, so save a macro-enabled copy as .xlsm.
Macro security can also prevent code from running. Only enable macros in a workbook you trust, and follow workplace security settings rather than lowering protection globally. When testing a macro that writes over values, work on a copy and start with a small sample range. Remember that Ctrl+Z cannot undo changes made by VBA.
For a worksheet function, use a clear function name such as ReverseText and avoid naming it after an existing Excel function. If the result seems unchanged, check whether the input has one character or is blank; those strings are naturally unchanged by reversal. If only some cells change under the macro, confirm that the remaining cells are text constants rather than formulas, numbers, or errors, all of which the sample deliberately skips.
Frequently Asked Questions
Is there a built-in Excel formula called REVERSE?
No. Excel does not provide a standard worksheet function named REVERSE for reversing characters. In VBA, use StrReverse; for a worksheet formula, use a UDF or a formula-based approach available in supported Excel versions.
Can I use StrReverse directly in a cell?
Not as a built-in worksheet function. Put a public function that calls StrReverse in a standard VBA module, then call that function from a cell, for example =ReverseText(A2). The workbook must retain the VBA code and macros must be permitted.
Does the macro reverse numbers too?
The sample macro does not. It processes text constants and skips numeric values, formulas, and errors. You can write different conversion logic, but decide how to treat dates, decimals, and leading zeros before doing so.
Can Excel for the web run this VBA?
No. Excel for the web does not run VBA macros or VBA UDFs. Open the macro-enabled workbook in desktop Excel to use the VBA code; a supported worksheet formula is an option when the workbook must work in a browser.
Will StrReverse keep emoji and accented characters intact?
Simple text generally reverses as expected, but some visible characters consist of multiple Unicode units. Reversing those units independently can split a combined symbol or accent. Test the actual characters in your data if Unicode text must remain visually intact.
Related
- Resolving Circular References in Excel
- Repeat Print Area in Excel: Repeat Rows on Every Page
- Remove Text to Columns in Excel: Undo or Reverse It
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA PivotTable Range: Source, Output, and ExamplesIn VBA, a PivotTable’s source range is the worksheet data used to build or refresh it, while its…
- Excel VBA Unprotect Sheet: Safe Methods and ExamplesTo unprotect a worksheet with VBA in Excel for Microsoft 365 on Windows, use Worksheet.Unprotect and supply the…
- Excel VBA Hex String: Convert Numbers to HexadecimalIn Excel VBA, use the Hex function to turn a number into a hexadecimal string: Hex(255) returns FF.…
- Excel VBA Link to Worksheet: Methods and ExamplesIn Excel VBA, link to a worksheet by referring to it with ThisWorkbook.Worksheets("SheetName"); to create a clickable link…
- Excel VBA: Protect a Sheet and Manage Permissions SafelyYes. In Excel VBA, protect a worksheet with its Protect method, for example Worksheets("Entry").Protect Password:="demo-pass". Add options such…
- VBA For Loop Backwards: Syntax, Examples, and Common FixesTo make a VBA For loop count backwards, give it a negative Step, such as For i =…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.