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 Office 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!
Make your macros too long, and Excel may just refuse to run them at all. This tip explains what the limit is for macros ...
Discover MoreOne of the powerful programming structures available in VBA is the Select Case structure. This tip explains how you can ...
Discover MoreWant to have you macro completely hide the Excel interface? You can do so by using the Visible property for the Excel ...
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 © 2022 Sharon Parq Associates, Inc.
Comments