reverse text

L

levyta

I try to write a macro that reverse the following text :


"abc 12/34"

to

"cba 12/34"

i.e., it reverses only the text, not the numbers.
 
B

BrianB

'-------------------------------------
Sub test()
Dim MyString As String
Dim NewString As String
MyString = "abc 12/34"
NewString = Mid(MyString, 3, 1) & Mid(MyString, 2, 1) _
& Mid(MyString, 1, 1) & Right(MyString, 6)
MsgBox (MyString & vbCr & NewString)
End Sub
'------------------------------------------
 
L

levyta

I guess I did not explain my self right.

The text is not a constant, but random variable, for example:

a street address.
 
D

Dave Peterson

One way is to look for that space and then reverse it based on where you found
it.

Option Explicit
Sub testme()
Dim myCell As Range
Dim SpacePosition As Long

For Each myCell In Selection.Cells
SpacePosition = InStr(1, myCell.Value, " ")
If SpacePosition > 0 Then
myCell.Value = Mid(myCell.Value, SpacePosition + 1) _
& " " & Left(myCell.Value, SpacePosition - 1)
End If
Next myCell

End Sub

Select a range and try it out.

don't select cells that are formulas--you'll lose them if you run this.
 
L

levyta

the macro does not do what I hoped for.

suppose we have:

abc 12/34 efg

the macro gives:

12/34 efg abc


and not:

cba 12/34 gfe


hope some one can help me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top