MrExcel Message Board


Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old May 9th, 2009, 10:51 PM   #1
lordriel
 
Join Date: Nov 2005
Posts: 32
Default Separating first and last names - vba

I've seen several methods for separating names, or text, from one cell to two or more. However, none of them give a vba answer.

How would I code it, were it to be done when hitting a command button?
__________________
The best way out is through!
lordriel is offline   Reply With Quote
Old May 9th, 2009, 11:06 PM   #2
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,635
Default Re: Separating first and last names - vba

Hello,

That really depends on how complex your names are. Is it always two names with a space? If so, record a Macro using Text-to-columns (data). Use a space as your delimiter.

If you have middle names, and suffixes, etc... You're going to have to work quite a bit harder, I'm afraid... But, that has been posted here, to date.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 9th, 2009, 11:20 PM   #3
lordriel
 
Join Date: Nov 2005
Posts: 32
Default Re: Separating first and last names - vba

This works, and would be functional if I change up a few other levels of code - which I may have to do.

I am clocking the data from one cell on one sheet into two cells on another in another workbook.

I tried the following code, with mixed results (probably because I'm not understanding everything it's doing):

lname = Right(Worksheets("Import").Cells(j, 2), Len(Worksheets("Import").Cells(j, 2)) - InStrRev(Worksheets("Import").Cells(j, 2), " "))

FName = Left(Worksheets("Import").Cells(j, 2), Len(Trim(Worksheets("Import").Cells(j, 2))) - InStrRev(Worksheets("Import").Cells(j, 2), Trim(lname)))

Worksheets("import").Cells(j, 12) = lname
Worksheets("import").Cells(j, 13) = FName


(Data in the original field is First Name Last Name)

This gave me the last name, clean and clear. But the first name is either truncated or includes parts of the last name. I know it's because I'm defining the length of the result based on the length of the last name...but haven't noodled out how to remedy it.

Thanks for looking...

lr
__________________
The best way out is through!
lordriel is offline   Reply With Quote
Old May 9th, 2009, 11:22 PM   #4
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,635
Default Re: Separating first and last names - vba

Text-to-Columns is an extremely fast algorithm, why not use it, and then cut and paste wherever you want, with code (you can disable ScreenUpdating while it processes)? That's how you'd want to do it by hand, or by code.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 9th, 2009, 11:25 PM   #5
VoG
MrExcel MVP
 
VoG's Avatar
 
Join Date: Jun 2002
Location: UK 51°34'30.38"N, 0°24'59.13"W
Posts: 24,011
Default Re: Separating first and last names - vba

With code and assuming that the names are in column A

Code:
Sub SplitNames()
Dim LR As Long, i As Long, X As Variant
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
    With Range("A" & i)
        X = Split(.Value)
        .Offset(, 1).Resize(, UBound(X) + 1).Value = X
    End With
Next i
End Sub
__________________
HTH, Peter
VoG is offline   Reply With Quote
Old May 9th, 2009, 11:31 PM   #6
lordriel
 
Join Date: Nov 2005
Posts: 32
Default Re: Separating first and last names - vba

Like a charm!

Thanks to both of you for your help!
__________________
The best way out is through!
lordriel is offline   Reply With Quote
Old May 9th, 2009, 11:43 PM   #7
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,635
Default Re: Separating first and last names - vba

Another approach, although very similar, would be to use an Array instead of looping through Ranges, toggle your ScreenUpdating, as mentioned, and use a String Array, e.g.,

Code:
Sub foo()
Dim varArr As Variant
Dim i As Long, strArr() As String
Application.ScreenUpdating = False
With Worksheets(1)
    Let varArr = .Range(.Cells(1, 1), _
        .Cells(.Rows.Count, 1).End(xlUp)).Value
    If IsArray(varArr) Then
        For i = LBound(varArr, 1) To UBound(varArr, 1)
            Let strArr() = Split(varArr(i, 1))
            Let .Cells(i, 2).Resize( _
                , UBound(strArr) + 1).Value = strArr
            Erase strArr
        Next
    Else: .Cells(1, 2).Resize(, 2).Value = Split(varArr)
    End If
End With
Application.ScreenUpdating = True
End Sub
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog

Last edited by NateO; May 10th, 2009 at 12:01 AM. Reason: Missed a dot
NateO is offline   Reply With Quote
Old May 10th, 2009, 06:45 AM   #8
Peter_SSs
MrExcel MVP
Moderator
 
Peter_SSs's Avatar
 
Join Date: May 2005
Location: Macksville, Australia
Posts: 14,219
Default Re: Separating first and last names - vba

Quote:
Originally Posted by lordriel View Post
Like a charm!
I'm assuming you are referring to VoG's code (which does work like a charm) but I'm wondering what happened to Nate's suggestion of using excel's built-in functionality for this sort of job, namely 'Text to Columns'?

If VoG's looping code does the job, then as far as I can see this 'one-hit' Text-to-Columns method should do too. Assumes data starts in cell A1.

Sub Split_Names()
    Columns("A").TextToColumns Destination:=Range("B1"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Space:=True
End Sub
__________________
Hope this helps, good luck.
Peter
Peter_SSs is offline   Reply With Quote
Old May 10th, 2009, 09:31 PM   #9
NateO
MrExcel MVP
Admin
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,635
Default Re: Separating first and last names - vba

That's exactly what I meant, Peter - Excel has native functionality (nice functionality) to do this, and it's mind-boggling fast.

Try that on 60,000 rows of data (fast). Whoever wrote that algorithm gets an A+ from me.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT +1. The time now is 04:50 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
All contents Copyright 1998-2009 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes