Inserting and deleting spaces with a macro

  • Thread starter Thread starter crowdx42
  • Start date Start date
C

crowdx42

Ok,
so I have names in a column which I want to both insert a space afte
a "," and then delete everything after the second word space. i.e
James,Patrick Coogan needs to be
James, Patrick
Can this be done?
Thanks in advance
You guys have been very patient and I very much appreciate all th
help.
Patrick:
 
Assuming the only comma and space are as indicated, this UDF should do it.

Public Function FixName(argRange As Range) As String
FixName = Replace(Split(argRange, " ")(0), ",", ", ")
End Function

You should add error checking to ensure that argRange is a single cell.

NickHK
 
=LEFT(A1,FIND(",",A1,1)-1) & ", " & MID(A1,FIND(",",A1,1)+1,(FIND("
",A1,1)-FIND(",",A1,1)))

or

Public Function CleanUpString(nameInCell As String) As String

CleanUpString = Left(nameInCell, InStr(1, nameInCell, ",",
vbTextCompare) - 1) & ", " & Mid(nameInCell, InStr(1, nameInCell, ",",
vbTextCompare) + 1, (InStr(1, nameInCell, " ", vbTextCompare) - InStr(1,
nameInCell, ",", vbTextCompare)))

End Function
 
NickHK said:
Assuming the only comma and space are as indicated, this UDF should do it.

Public Function FixName(argRange As Range) As String
FixName = Replace(Split(argRange, " ")(0), ",", ", ")
End Function
Way to go.
 
You can use worksheet functions to do the same.

=CONCATENATE(LEFT(A1,FIND(",",A1,1))," ",MID(A1,FIND(",",A1,1)+2,FIND("
",A1,FIND(",",A1))))

if A1 contains the name.
 
Hi Try this one
A1: James,Patrick Coogan
B1: =SUBSTITUTE(LEFT(A1,FIND(" ",A1)),",",", ")
 
You could select your range
edit|replace
what: (spacebar)* (two characters total)
with: (leave blank)
replace all

edit|Replace
what: ,
with: ,(spacebar)
replace all

If you needed code:
Option Explicit
Sub testme()
With Selection
.Replace What:=" *", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False
.Replace What:=",", Replacement:=", ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False
End With
End Sub
 
Excellent stuff,
Dave's worked perfect for me, saved me a lot of time.
Thanks so much :)
Patrick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top