Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-28-2017, 02:17 PM
delivery delivery is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2016
Novice
Separate First and Last and how to add to existing Macro
 
Join Date: Aug 2017
Posts: 4
delivery is on a distinguished road
Default Separate First and Last and how to add to existing Macro

I recorded a macro to do some basic functions but need to add in something to it.



I have names in column I. Some are first last , first middle last, first middle (two names) last

I just want the first name in column J and the last name in column K ignoring whatever middles names are there if any. How do I tell it to do it to the entire I column?

Where would I insert that into my record macro? Right before End Sub?
Reply With Quote
  #2  
Old 08-28-2017, 09:20 PM
NoSparks NoSparks is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

try this, hopefully it's commented enough that you can follow

Code:
Sub YourExistingMacro()
    'declare variables
    Dim lr As Long          'last row used in the column
    Dim rng As Range        'the range to operate on
    Dim cel As Range        'individual cells of that rng
    Dim ws As Worksheet     'the worksheet being worked on
    Dim a As Variant        'array to hold individual words of the cell
    
    '
    '~~~~~~~~~~~~~~~~~~~~~~~~~
    ' your existing code here
    '~~~~~~~~~~~~~~~~~~~~~~~~~
    '
    
Set ws = Sheets("Sheet1")
With ws
    'determine the last row of column I
    lr = .Range("I" & Rows.Count).End(xlUp).Row
    'establish the range to operate on
    Set rng = .Range("I2:I" & lr)
    'step through the range one cell at a time
    For Each cel In rng
        'provided the cell isn't blank
        If cel.Value <> "" Then
            'load the individual words into array
            a = Split(cel.Value, " ")
            'write the first element of the array one column right of cel
            cel.Offset(, 1).Value = a(LBound(a))
            'write the last element of the array two columns right of cel
            cel.Offset(, 2).Value = a(UBound(a))
        End If
    'move to the next cell
    Next cel
End With
    
End Sub
Reply With Quote
  #3  
Old 08-28-2017, 11:33 PM
ArviLaanemets ArviLaanemets is offline Separate First and Last and how to add to existing Macro Windows 8 Separate First and Last and how to add to existing Macro Office 2016
Expert
 
Join Date: May 2017
Posts: 932
ArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant futureArviLaanemets has a brilliant future
Default

Having a full name in single column may be really a can of worms!

When you are sure both first and last names are always single names, you can use worksheet formulas - no need for VBA:
a) for leftmost name
=LEFT($A2,IF(ISERROR(FIND(" ",$A2;1)),LEN($A2),FIND(" ",$A2,1)))
b) for rightmost name
=IF(ISERROR(FIND(" ",$A2,1)),"",MID($A2,FIND("|",IF(ISERROR(FIND( " ",$A2,1)),"",SUBSTITUTE($A2," ","|",FIND(" ",$A2,1)-1)))+1,250))

When there is only a single name in FullName column ($A), the 2nd formula returns an empty string.
Instead of character "|" you can use any other character not being used in any of names.

Of course the formulas aren't helping when people have many-part first names (NB! Not first and middle names, but several first names!) or many-part last names (p.e. some women add husbands family name to her maiden family name) - but then doesn't VBA help too. And I hate to think about Spanish names at all
Reply With Quote
  #4  
Old 08-30-2017, 10:28 AM
delivery delivery is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2016
Novice
Separate First and Last and how to add to existing Macro
 
Join Date: Aug 2017
Posts: 4
delivery is on a distinguished road
Default

We already are using a macro to clean up the excel file we are given each month so ideally we just want to add extra code to have to also separate the names so we do not have to do extra formula work and the last part is to split up these names but as previous post mentions there are a lot of Spanish names so there are multiple names not just first last.

The file has a combination of different name strings.

I basically want to get the first name and just the last name and ignore whatever is in the middle. Possible?
Reply With Quote
  #5  
Old 08-30-2017, 11:01 AM
NoSparks NoSparks is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Did you try the suggestion in post #2 ?
Reply With Quote
  #6  
Old 08-30-2017, 11:03 AM
delivery delivery is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2016
Novice
Separate First and Last and how to add to existing Macro
 
Join Date: Aug 2017
Posts: 4
delivery is on a distinguished road
Default

How do I put an excel formula into the macro?
Reply With Quote
  #7  
Old 08-30-2017, 11:12 AM
NoSparks NoSparks is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Have you thought about just running the macro?
Reply With Quote
  #8  
Old 08-30-2017, 11:22 AM
delivery delivery is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2016
Novice
Separate First and Last and how to add to existing Macro
 
Join Date: Aug 2017
Posts: 4
delivery is on a distinguished road
Default

I am running a macro now to format the file we get currently but not sure how I would put actual formulas in the macro.
Reply With Quote
  #9  
Old 08-30-2017, 11:33 AM
NoSparks NoSparks is offline Separate First and Last and how to add to existing Macro Windows 7 64bit Separate First and Last and how to add to existing Macro Office 2010 64bit
Excel Hobbyist
 
Join Date: Nov 2013
Location: British Columbia, Canada
Posts: 842
NoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of lightNoSparks is a glorious beacon of light
Default

Just run the macro above exactly as is and see what happens.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro for creating word docs from existing template ferocious12 Word VBA 5 02-04-2017 09:57 PM
Separate First and Last and how to add to existing Macro Tweak Macro to Save Each Page of Word Document as Separate PDF or Doc File? Hewg74 Word VBA 3 08-22-2016 05:20 PM
Separate First and Last and how to add to existing Macro Can I use the filter function to separate letters into separate files? drhauser Mail Merge 2 12-14-2011 02:18 PM
How do you update existing Outlook calendar item from Word with macro? Joe Patrick Word VBA 0 07-09-2011 05:32 AM
Any easy way to separate a Word document into separate files? SamHelm Word 0 08-21-2010 05:29 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:58 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft