LEN formula with first/last names

brooksc29

Active Member
Joined
Jul 25, 2010
Messages
333
Right now I have this formula in a macro for formatting a workbook that will look at a cell with a first and last name and put the two names into two different cells, then only keep the cell with the last name.

How can I change it to keep the first initial of the first name and the last name?

this is the formula:
=IF(LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1], "" "", """")) = 2, MID(RC[-1],FIND("" "",RC[-1])+1,FIND("" "",RC[-1],FIND("" "",RC[-1])+1)-(FIND("" "",RC[-1])+1)),RIGHT(RC[-1],LEN(RC[-1])-FIND(""*"",SUBSTITUTE(RC[-1],"" "",""*"",LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1],"" "",""""))))))
 
well, if it's working for you, how can I work it into the macro I already have built, or what piece of that macro needs to be replaced? Putting it in it's own module won't work for me as this needs to be applied to over 200 worksheets in the workbook
 
Upvote 0

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
also, keep in mind this data is imported from IE from a website and the URLs for where the data comes from are manipulated and altered from a "setup" page in the workbook. The user chooses a few variables that change the URLs and the new data is imported... that is why I need to use VBA code.
 
Upvote 0
Change the line of code to

myModifiedName = Left(myName, 1) & ". " & Right(myName, Len(myName) - InStr(myName, " "))
 
Upvote 0
Try This and see how it works

code:
Sub gpNames()
Dim myName As String
Dim myModifiedName As String
Dim pvRow As Integer
Dim pvCol As Integer

pvCol = ActiveCell.Column
pvRow = ActiveCell.Row
Do Until Cells(pvRow, pvCol) = ""
myName = Cells(pvRow, pvCol)
myModifiedName = Left(myName, 1) & ". " & Right(myName, Len(myName) - InStr(myName, " "))
Cells(pvRow, pvCol + 1) = myModifiedName
pvRow = pvRow + 1
Loop
End Sub
 
Upvote 0
do you know of any way to just add to the formula I already have

=IF(LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1], "" "", """")) = 2, MID(RC[-1],FIND("" "",RC[-1])+1,FIND("" "",RC[-1],FIND("" "",RC[-1])+1)-(FIND("" "",RC[-1])+1)),RIGHT(RC[-1],LEN(RC[-1])-FIND(""*"",SUBSTITUTE(RC[-1],"" "",""*"",LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1],"" "",""""))))))"

so that this pulls the last name and first initial?

I think there is too much in the VBA code that I already have set up to make your piece work without having to change a lot. It would be easiest if I can just change what I already have
 
Upvote 0
Try This and see how it works

code:
Sub gpNames()
Dim myName As String
Dim myModifiedName As String
Dim pvRow As Integer
Dim pvCol As Integer

pvCol = ActiveCell.Column
pvRow = ActiveCell.Row
Do Until Cells(pvRow, pvCol) = ""
myName = Cells(pvRow, pvCol)
myModifiedName = Left(myName, 1) & ". " & Right(myName, Len(myName) - InStr(myName, " "))
Cells(pvRow, pvCol + 1) = myModifiedName
pvRow = pvRow + 1
Loop
End Sub

sub this out for my current macro?
 
Upvote 0
can you paste your whole macro here so we can see why you just do not replace it with one of the solutions that have benn posted?
 
Upvote 0
first i run this macro
Columns("B:B").Select
Selection.EntireColumn.Insert

Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1], "" "", """")) = 2, MID(RC[-1],FIND("" "",RC[-1])+1,FIND("" "",RC[-1],FIND("" "",RC[-1])+1)-(FIND("" "",RC[-1])+1)),RIGHT(RC[-1],LEN(RC[-1])-FIND(""*"",SUBSTITUTE(RC[-1],"" "",""*"",LEN(RC[-1])-LEN(SUBSTITUTE(RC[-1],"" "",""""))))))"

Selection.Copy
Selection.CurrentRegion.Select
Selection.SpecialCells(xlCellTypeBlanks).Select
ActiveSheet.Paste
Application.CutCopyMode = False


then this one
Selection.CurrentRegion.Select
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'
' This part moves to Cell A1 then deletes column A

Range("A1").Select
Selection.EntireColumn.Delete
 
Upvote 0
the first macro does this

This macro will insert a new column B, go to B2 of the inserted column and enter a formula that extracts the last name in a string in a cell and it then inserts the formula in all blank cells beside cells that contain first and last names.
 
Upvote 0
So the first portion of the code will create the First Initial and Last Name.
The second portion just gets rid of the formulas
If this is true then the following code will replace your entire macro. You will not need to run two macros.

code:
Sub gpNames()
Dim myName As String
Dim myModifiedName As String
Dim pvRow As Integer
Dim pvCol As Integer

Columns("B:B").Select
Selection.EntireColumn.Insert

pvCol = 1
pvRow = ActiveCell.Row
Do Until Cells(pvRow, pvCol) = ""
myName = Cells(pvRow, pvCol)
myModifiedName = Left(myName, 1) & ". " & Right(myName, Len(myName) - InStr(myName, " "))
Cells(pvRow, pvCol + 1) = myModifiedName
pvRow = pvRow + 1
Loop
Range("A1").Select
Selection.EntireColumn.Delete
End Sub

\code:
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top