Thread: Reversing Words
View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Rocky McKinley Rocky McKinley is offline
external usenet poster
 
Posts: 102
Default Reversing Words

This is another alternative...

Sub ReverseWords()
For Each cell In Range("B5:B105")
cell.Value = ReturnWord(cell.Value, 2) & " " & ReturnWord(cell.Value, 1)
Next
End Sub

Function ReturnWord(MainString As Variant, WordNumber As Integer)
Dim LastChr As String, StartChrReturn As Integer, EndChrReturn As Integer,
Cnt As Integer, _
I As Integer, LeftWord As String, RightWord As String
MainString = " " & Trim(MainString) & " ": LastChr = "": Cnt = 0

For I = 1 To Len(MainString)
If Mid(MainString, I, 1) = " " And LastChr < " " Then
Cnt = Cnt + 1
If Cnt = WordNumber Then StartChrReturn = I
If Cnt = WordNumber + 1 Then EndChrReturn = I
End If
LastChr = Mid(MainString, I, 1)
Next I
On Error GoTo ErrorHandler:
ReturnWord = Trim(Mid(MainString, StartChrReturn, EndChrReturn -
StartChrReturn))
Exit Function
ErrorHandler:
ReturnWord = ""
End Function

--
Regards,
Rocky McKinley


"Todd Huttenstine" wrote in message
...
Hey guys in Range B5:B105 I have a list of names. They
are formatted as Lastname/Firstname. I need a code that
will go through all the names in this range and reverse
the order of the names.

For example... Huttenstine Todd needs to turn into Todd
Huttenstine

Tom gave me a code that took the commas out, but this time
there are no commas to take out.

Thank you.


Todd