View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Ron Rosenfeld
 
Posts: n/a
Default Text string inverting

On Thu, 26 Jan 2006 11:16:02 -0800, cagolden2003
wrote:

I am importing text, and it comes in reading from right to left.
How can I change it to read from left to right?

I know that the formula below will invert two words, but how do you do more
than two words?

=RIGHT(A1,LEN(A1)-FIND(" ",A1))&" "&LEFT(A1,FIND(" ",A1)-1)

Let's say I wanted "Right To Left" to read "Left To Right" or "John J Doe"
to read "Doe J John"....


You can do that with a UDF.

<alt<F11 opens the VB Editor.

Ensure your project is highlighted in the project explorer window, then
Insert/Module and paste the code below into the window that opens.

You can then use the function =RevWords(cell_ref) in any cell and it should do
what you describe. This requires a recent enough version of Excel that you
have VBA6. If you have an older version of Excel, we will have to substitute
for the Join and Split functions.

======================
Function RevWords(str As String) As String
Dim t1() As String, t2() As String
Dim l As Long, i As Long

t1 = Split(str)
ReDim t2(UBound(t1))

For l = UBound(t1) To 0 Step -1
t2(i) = t1(l)
i = i + 1
Next l

RevWords = Join(t2)

End Function
=========================
--ron