+ Reply to Thread
Results 1 to 8 of 8

Inserting and deleting spaces with a macro

  1. #1
    Registered User
    Join Date
    08-18-2006
    Posts
    68

    Inserting and deleting spaces with a macro

    Ok,
    so I have names in a column which I want to both insert a space after 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 the help.
    Patrick

  2. #2
    NickHK
    Guest

    Re: Inserting and deleting spaces with a macro

    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

    "crowdx42" <crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com> wrote in
    message news:crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com...
    >
    > Ok,
    > so I have names in a column which I want to both insert a space after
    > 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 the
    > help.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile:

    http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=574054
    >




  3. #3
    moon
    Guest

    Re: Inserting and deleting spaces with a macro


    =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



    "crowdx42" <crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com> schreef
    in bericht news:crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com...
    >
    > Ok,
    > so I have names in a column which I want to both insert a space after
    > 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 the
    > help.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile:
    > http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=574054
    >




  4. #4
    moon
    Guest

    Re: Inserting and deleting spaces with a macro


    "NickHK" <TungCheWah@Invalid.com> schreef in bericht
    news:%23ZQmC3bxGHA.1272@TK2MSFTNGP05.phx.gbl...
    > 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.



  5. #5
    Jude
    Guest

    Re: Inserting and deleting spaces with a macro

    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.

    "crowdx42" <crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com> wrote in
    message news:crowdx42.2cxhbd_1156227604.3714@excelforum-nospam.com...
    >
    > Ok,
    > so I have names in a column which I want to both insert a space after
    > 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 the
    > help.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile:
    > http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=574054
    >




  6. #6
    Muhammed Rafeek M
    Guest

    RE: Inserting and deleting spaces with a macro

    Hi Try this one
    A1: James,Patrick Coogan
    B1: =SUBSTITUTE(LEFT(A1,FIND(" ",A1)),",",", ")

    "crowdx42" wrote:

    >
    > Ok,
    > so I have names in a column which I want to both insert a space after
    > 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 the
    > help.
    > Patrick
    >
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile: http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=574054
    >
    >


  7. #7
    Dave Peterson
    Guest

    Re: Inserting and deleting spaces with a macro

    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


    crowdx42 wrote:
    >
    > Ok,
    > so I have names in a column which I want to both insert a space after
    > 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 the
    > help.
    > Patrick
    >
    > --
    > crowdx42
    > ------------------------------------------------------------------------
    > crowdx42's Profile: http://www.excelforum.com/member.php...o&userid=37749
    > View this thread: http://www.excelforum.com/showthread...hreadid=574054


    --

    Dave Peterson

  8. #8
    Registered User
    Join Date
    08-18-2006
    Posts
    68
    Excellent stuff,
    Dave's worked perfect for me, saved me a lot of time.
    Thanks so much
    Patrick

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1