Written by Allen Wyatt (last updated August 29, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Did you ever want to reverse the contents of what is contained in a cell? Using the StrReverse function in VBA, you can easily change "My text" to "txet yM."
Dim sTemp As String sTemp = "My text" sTemp = StrReverse(sTemp)
When completed, the text in the sTemp variable will be equal to "txet yM." If you want to reverse the contents of a cell in a worksheet, you could use the following:
Selection = StrReverse(Selection)
This reverses whatever cell is selected in the worksheet. I find it helpful, though, to build out the usage just a bit to make sure that only a single cell is selected and that the cell does not contain a formula:
If Selection.Count =1 Then If Not Selection.HasFormula Then Selection = StrReverse(Selection) End If
This macro only affects a single selected cell, and it will not make any changes to a cell that contains a formula.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (3451) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Reversing Cell Contents.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
When creating user forms for use in Excel, you are provided with a range of controls you can add, including check boxes. ...
Discover MoreWhen creating macros, you'll often have a need to select different cells in the worksheet. Here's how to select the first ...
Discover MoreWhen running a macro, have you ever seen Excel appear to stop responding? This can be frustrating, but there are a couple ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2022-04-16 12:24:18
J. Woolley
@Rkeev
I'm not sure what you want, but this macro will reverse all text string constants in the selected range. It skips cells that have a formula or a numeric or logical constant.
Sub ReverseTextInSelection()
For Each cell In Selection
t = WorksheetFunction.IsText(cell.Value)
If t And (Not cell.HasFormula) Then
cell.Value = StrReverse(cell.Value)
End If
Next cell
End Sub
2022-04-15 08:16:59
Rkeev
I know you convert this macro to a loop but Is there a way to do this in memory? Such as copy a range into an array then perform the reverse macro, then return the array to the spreadsheet without waiting for a loop?
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments