Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reverse order of data in a cell

Status
Not open for further replies.

QuickC

Military
Dec 2, 2004
1
I am trying to figure how to reverse data in a string located in a cell. The string elements are separated by commas. For example, the string is : US=country,State=NorthCarolina,City =Charlotte. I want to reverse it to read City=Charlotte,State=NorthCarolina,Country=US.

Thanks
 
Replies continue below

Recommended for you

You might try using the Split function to separate the entries by the comma, and then rebuild the string by walking the array from UBound to LBound.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Could use VBasic function, am sure you could optimise code but here is one solution :

Function reverse(A As String)

Dim B(30)
A = A + ","
Do While Len(A) > 0
i = i + 1
num = Application.WorksheetFunction.Find(",", A)
B(i) = Left(A, num - 1)
A = Right(A, Len(A) - num)
Loop
For j = i To 1 Step -1
reverse = reverse + B(j) + ","
Next j
reverse = Left(reverse, Len(reverse) - 1)

End Function
 
You could also use an array entered formula:

=RIGHT(B2,MATCH(",",MID(B2,LEN(B2)-ROW(INDIRECT("1:"&LEN(B2))),1),0))&","&MID(B2,FIND(",",B2)+1,FIND(",",B2,FIND(",",B2)+1)-FIND(",",B2))&LEFT(B2,FIND(",",B2)-1)

Here the text to reverse is in cell B2, and you enter the formula with the (Control + Shift + Enter) keys. And each time you edit this. You don't put in the curly {} Excel does this automatically if array entered.

Regards
Robert
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor