Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
kai kai is offline
external usenet poster
 
Posts: 5
Default How to seperate lastname and firstname?

Hi, All
I have a column "Name", it has full name, format "lastname, firstname". I
would like to write code to split into "Lastname" and "Firstname" column.
How do write code to seperate lastname when I see ","?

Thanks

kai


  #2   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default How to seperate lastname and firstname?

Hi kai,

First of all, this task can be performed by simple formulae:

last name =LEFT(TRIM(A1),FIND(",",TRIM(A1))-1)
first name =MID(TRIM(A1),FIND(",",TRIM(A1))+2,LEN(TRIM(A1)))

or via the menu DataText to column... etc.

If you still want to use the code and you use XL2000 or later you could try
this:


Sub Test()
With ActiveSheet
Set rng = .Range(.Cells(1, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))
End With
For Each c In rng
If c Like "*,*" Then
myNames = Split(c, ",")
c.Offset(, 1) = Trim(myNames(0))
c.Offset(, 2) = Trim(myNames(1))
End If
Next
End Sub



Regards,
KL


"kai" wrote in message
nk.net...
Hi, All
I have a column "Name", it has full name, format "lastname, firstname". I
would like to write code to split into "Lastname" and "Firstname" column.
How do write code to seperate lastname when I see ","?

Thanks

kai



  #3   Report Post  
Posted to microsoft.public.excel.programming
KL KL is offline
external usenet poster
 
Posts: 201
Default How to seperate lastname and firstname?

Actually, the formulae can be simplified:

last name =TRIM(LEFT(A1,FIND(",",A1)-1))
first name =TRIM(MID(A1,FIND(",",A1)+1,LEN(A1)))

Regards,
KL


"KL" wrote in message
...
Hi kai,

First of all, this task can be performed by simple formulae:

last name =LEFT(TRIM(A1),FIND(",",TRIM(A1))-1)
first name =MID(TRIM(A1),FIND(",",TRIM(A1))+2,LEN(TRIM(A1)))

or via the menu DataText to column... etc.

If you still want to use the code and you use XL2000 or later you could
try this:


Sub Test()
With ActiveSheet
Set rng = .Range(.Cells(1, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))
End With
For Each c In rng
If c Like "*,*" Then
myNames = Split(c, ",")
c.Offset(, 1) = Trim(myNames(0))
c.Offset(, 2) = Trim(myNames(1))
End If
Next
End Sub



Regards,
KL


"kai" wrote in message
nk.net...
Hi, All
I have a column "Name", it has full name, format "lastname, firstname".
I would like to write code to split into "Lastname" and "Firstname"
column.
How do write code to seperate lastname when I see ","?

Thanks

kai





  #4   Report Post  
Posted to microsoft.public.excel.programming
kai kai is offline
external usenet poster
 
Posts: 5
Default How to seperate lastname and firstname?

KL,
Thanks a lot, it works like magic.

Kai
"KL" wrote in message
...
Actually, the formulae can be simplified:

last name =TRIM(LEFT(A1,FIND(",",A1)-1))
first name =TRIM(MID(A1,FIND(",",A1)+1,LEN(A1)))

Regards,
KL


"KL" wrote in message
...
Hi kai,

First of all, this task can be performed by simple formulae:

last name =LEFT(TRIM(A1),FIND(",",TRIM(A1))-1)
first name =MID(TRIM(A1),FIND(",",TRIM(A1))+2,LEN(TRIM(A1)))

or via the menu DataText to column... etc.

If you still want to use the code and you use XL2000 or later you could
try this:


Sub Test()
With ActiveSheet
Set rng = .Range(.Cells(1, "A"), _
.Cells(.Rows.Count, "A").End(xlUp))
End With
For Each c In rng
If c Like "*,*" Then
myNames = Split(c, ",")
c.Offset(, 1) = Trim(myNames(0))
c.Offset(, 2) = Trim(myNames(1))
End If
Next
End Sub



Regards,
KL


"kai" wrote in message
nk.net...
Hi, All
I have a column "Name", it has full name, format "lastname, firstname".
I would like to write code to split into "Lastname" and "Firstname"
column.
How do write code to seperate lastname when I see ","?

Thanks

kai







  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 364
Default How to seperate lastname and firstname?

i know you wanted to write code to do this, but you can also use the text to
columns menu item
select column
click data/text to columns
select delimited and next
click the box next to comma click finish
you will have the names in separate columns.

you want code, just record a macro while you're doing this operation.

--


Gary


"kai" wrote in message
nk.net...
Hi, All
I have a column "Name", it has full name, format "lastname, firstname". I
would like to write code to split into "Lastname" and "Firstname" column.
How do write code to seperate lastname when I see ","?

Thanks

kai



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
separate lastname from firstname enrico via OfficeKB.com Excel Worksheet Functions 3 April 14th 09 06:56 AM
LastName, FirstName covert to FirstName LastName Craig860 Excel Discussion (Misc queries) 4 January 14th 08 09:58 PM
How to change lastname, firstname to firstname lastname in Excel? mert Excel Discussion (Misc queries) 1 January 9th 08 06:06 PM
Seperate Cell Information (lastname, firstname) JFALK Excel Discussion (Misc queries) 6 August 5th 05 09:41 PM
LastName, FirstName format Todd Huttenstine[_2_] Excel Programming 2 December 15th 03 02:54 AM


All times are GMT +1. The time now is 12:39 AM.

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

About Us

"It's about Microsoft Excel"