![]() |
|
#1
|
|||
|
|||
![]()
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? |
#2
|
|||
|
|||
![]()
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 |
#3
|
|||
|
|||
![]()
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 ![]() |
#4
|
|||
|
|||
![]()
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? |
#5
|
|||
|
|||
![]()
Did you try the suggestion in post #2 ?
|
#6
|
|||
|
|||
![]()
How do I put an excel formula into the macro?
|
#7
|
|||
|
|||
![]()
Have you thought about just running the macro?
|
#8
|
|||
|
|||
![]()
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.
|
#9
|
|||
|
|||
![]()
Just run the macro above exactly as is and see what happens.
|
![]() |
|
![]() |
||||
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 |
![]() |
Hewg74 | Word VBA | 3 | 08-22-2016 05:20 PM |
![]() |
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 |