#1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 78
Default Switching Text

I have a huge list of names in either of the following ways:

PeterSmith or
Peter Smith

Is there an easier way (other than manually switch) to have the list in the
order like

Smith, Peter or at least
Smith Peter ?

The only way I can think of is to somehow recognize the capitals...

Thank you

Felix
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 10,593
Default Switching Text

Here's some code

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long, j As Long, k As Long
Dim iLastRow As Long
With ActiveSheet

iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To iLastRow
k = Len(.Cells(i, "A").Value)
For j = k To 2 Step -1
If Mid(.Cells(i, j).Value, 1) = " " Or _
Mid(.Cells(i, "A").Value, j, 1) = UCase(Mid(.Cells(i,
"A").Value, j, 1)) Then
.Cells(i, "A").Value = Right(.Cells(i, "A").Value, k - j
+ 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)
Exit For
End If
Next j
Next i

End With

End Sub



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Felix" wrote in message
...
I have a huge list of names in either of the following ways:

PeterSmith or
Peter Smith

Is there an easier way (other than manually switch) to have the list in
the
order like

Smith, Peter or at least
Smith Peter ?

The only way I can think of is to somehow recognize the capitals...

Thank you

Felix



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 78
Default Switching Text

Hi Bob,

I tried to copy to the code. All of the following part comes out in red:


If Mid(.Cells(i, j).Value, 1) = " " Or _
Mid(.Cells(i, "A").Value, j, 1) = UCase(Mid(.Cells(i,
"A").Value, j, 1)) Then
.Cells(i, "A").Value = Right(.Cells(i, "A").Value, k - j
+ 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)



What do I do wrong?


Felix


"Bob Phillips" wrote:

Here's some code

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long, j As Long, k As Long
Dim iLastRow As Long
With ActiveSheet

iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To iLastRow
k = Len(.Cells(i, "A").Value)
For j = k To 2 Step -1
If Mid(.Cells(i, j).Value, 1) = " " Or _
Mid(.Cells(i, "A").Value, j, 1) = UCase(Mid(.Cells(i,
"A").Value, j, 1)) Then
.Cells(i, "A").Value = Right(.Cells(i, "A").Value, k - j
+ 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)
Exit For
End If
Next j
Next i

End With

End Sub



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Felix" wrote in message
...
I have a huge list of names in either of the following ways:

PeterSmith or
Peter Smith

Is there an easier way (other than manually switch) to have the list in
the
order like

Smith, Peter or at least
Smith Peter ?

The only way I can think of is to somehow recognize the capitals...

Thank you

Felix




  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Switching Text

Bob's code got hit by a line wrap problem in the newsgroup:

Option Explicit
Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long, j As Long, k As Long
Dim iLastRow As Long
With ActiveSheet
iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To iLastRow
k = Len(.Cells(i, "A").Value)
For j = k To 2 Step -1
If Mid(.Cells(i, j).Value, 1) = " " _
Or Mid(.Cells(i, "A").Value, j, 1) _
= UCase(Mid(.Cells(i, "A").Value, j, 1)) Then
.Cells(i, "A").Value _
= Right(.Cells(i, "A").Value, k - j + 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)
Exit For
End If
Next j
Next i
End With
End Sub

Felix wrote:

Hi Bob,

I tried to copy to the code. All of the following part comes out in red:

If Mid(.Cells(i, j).Value, 1) = " " Or _
Mid(.Cells(i, "A").Value, j, 1) = UCase(Mid(.Cells(i,
"A").Value, j, 1)) Then
.Cells(i, "A").Value = Right(.Cells(i, "A").Value, k - j
+ 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)


What do I do wrong?

Felix

"Bob Phillips" wrote:

Here's some code

Public Sub ProcessData()
Const TEST_COLUMN As String = "A" '<=== change to suit
Dim i As Long, j As Long, k As Long
Dim iLastRow As Long
With ActiveSheet

iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
For i = 1 To iLastRow
k = Len(.Cells(i, "A").Value)
For j = k To 2 Step -1
If Mid(.Cells(i, j).Value, 1) = " " Or _
Mid(.Cells(i, "A").Value, j, 1) = UCase(Mid(.Cells(i,
"A").Value, j, 1)) Then
.Cells(i, "A").Value = Right(.Cells(i, "A").Value, k - j
+ 1) & _
", " & Left(.Cells(i, "A").Value, j - 1)
Exit For
End If
Next j
Next i

End With

End Sub



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Felix" wrote in message
...
I have a huge list of names in either of the following ways:

PeterSmith or
Peter Smith

Is there an easier way (other than manually switch) to have the list in
the
order like

Smith, Peter or at least
Smith Peter ?

The only way I can think of is to somehow recognize the capitals...

Thank you

Felix





--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,651
Default Switching Text

On Wed, 23 May 2007 23:55:01 -0700, Felix
wrote:

I have a huge list of names in either of the following ways:

PeterSmith or
Peter Smith

Is there an easier way (other than manually switch) to have the list in the
order like

Smith, Peter or at least
Smith Peter ?

The only way I can think of is to somehow recognize the capitals...

Thank you

Felix


Download and install Longre's free morefunc.xll add-in from
http://xcell05.free.fr

Then use this regular expression formula:

=REGEX.SUBSTITUTE(A1,"([A-Z][a-z]+).*?(\w+)","[2], [1]")

PeterSmith Smith, Peter
Peter Smith Smith, Peter

If there is more variability in the data than what you describe, some changes
in the regular expression may be necessary.
--ron
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text always switching to date format Brian Haberman Excel Discussion (Misc queries) 7 April 27th 07 03:24 PM
Lists Switching Yaroslav Excel Discussion (Misc queries) 2 August 22nd 06 08:30 AM
switching the worksheet? Eddie Excel Discussion (Misc queries) 1 April 24th 06 06:09 AM
VBA Switching Between Macros Jeff Excel Discussion (Misc queries) 4 November 5th 05 12:45 AM
Switching from Quattro Pro Michael Gula New Users to Excel 2 February 19th 05 03:19 AM


All times are GMT +1. The time now is 03:22 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"