+ Reply to Thread
Results 1 to 10 of 10

Fix names with code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2008
    Location
    New York
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    145

    Fix names with code

    Good day,
    I have a spread sheet where I've got to change the names, eg The name Paul Dechristian with this formula:
    =TRIM(RIGHT(B2,LEN(B2)-FIND(" ",B2))&", "&LEFT(B2,FIND(" ",B2)-1))
    would be Dechristian, Paul.
    I tried using a code Donkeyote used to solved a problem on this forum:
    Sub Example()
    With Range(Cells(1, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("IF(ISTEXT(" & .Address & "),PROPER(SUBSTITUTE(" & .Address & ","" "","", "",1)),REPT(" & .Address & ",1))")
    End With
    End Sub
    I changed it to this:
    Sub Example1()
    With Range(Cells(1, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(RIGHT(" & .Address & ",LEN(" & .Address & ")-FIND("" "",& .Address)),REPT(" & .Address & ",1))")
    End With
    End Sub
    but I get #VALUE! for all the cells.
    What am I doing wrong?
    Thanks,
    Xrull
    Last edited by Xrull; 02-17-2010 at 12:53 AM.

  2. #2
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Fix names with code

    Here is the Exapmle1 from below.

    Sub Example1()
    With Range(Cells(2, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("if(istext(" & .Address & "),trim(right(" & .Address & ",len(" & .Address & ")-find("" ""," & .Address & "))),rept(" & .Address & ",1))")
    End With
    End Sub
    this part
    -FIND("" "",& .Address)),REPT(" & .Address & ",1))"
    should be

    -find("" ""," & .Address & "))),rept(" & .Address & ",1))"

  3. #3
    Forum Contributor
    Join Date
    07-29-2008
    Location
    New York
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    145

    Re: Fix names with code

    mdbct,
    The code cuts off the first name, it doesn't change Paul Dechristian to Dechristian, Paul.
    Thanks,
    Xrull

  4. #4
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Fix names with code

    That's what the formula in the Example1 that you posted was set up to do. I didn't change the base formula in Example1.

  5. #5
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Fix names with code

    Try this one
    Sub Example1()
    
    Set myrange = Range(Cells(2, "B"), Cells(Rows.Count, "B").End(xlUp))
    For Each ccell In myrange
        With ccell
        .Value = Evaluate("trim(right(" & .Address & ",len(" & .Address & ")-find("" ""," & .Address & "))& "", "" & Left(" & .Address & ",find( "" ""," & .Address & ")-1))")
        End With
    Next
    
    End Sub
    I tried using the same method
    Sub Example2()
    With Range(Cells(2, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("trim(right(" & .Address & ",len(" & .Address & ")-find("" ""," & .Address & "))& "", "" & Left(" & .Address & ",find( "" ""," & .Address & ")-1))")
    End With
    End Sub
    This method didn't work. For some reason all cells were duplicates of cell B2.

  6. #6
    Forum Contributor
    Join Date
    07-29-2008
    Location
    New York
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    145

    Re: Fix names with code

    mdbct,
    The code worked OK,
    Thanks,
    Xrull

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Fix names with code

    FWIW, using formula based approach of:

    =MID(B2&", "&B2,FIND(" ",B2)+1,LEN(B2)+1)

    applied to Range simultaneously via Evaluate

    Sub Example()
    With Range(Cells(2, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("IF(ISTEXT(" & .Address & "),MID(" & .Address & "&"", ""&" & .Address & ",FIND("" ""," & .Address & ")+1,LEN(" & .Address & ")+1),REPT(" & .Address & ",1))")
    End With
    End Sub
    Last edited by DonkeyOte; 02-17-2010 at 03:28 PM. Reason: added the wrong native function prior to the VBA... duh.

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Fix names with code

    I missed this bit...

    Quote Originally Posted by mdbct View Post
    I tried using the same method

    Sub Example2()
    With Range(Cells(2, "B"), Cells(Rows.Count, "B").End(xlUp))
        .Value = Evaluate("trim(right(" & .Address & ",len(" & .Address & ")-find("" ""," & .Address & "))& "", "" & Left(" & .Address & ",find( "" ""," & .Address & ")-1))")
    End With
    End Sub

    This method didn't work. For some reason all cells were duplicates of cell B2.
    The initial ISTEXT test in the earlier example(s) actually serves a dual purpose

    1 - as implied it allows us to differentiate between Text and Non-Text entries such that we can apply an appropriate function to underlying value

    2 - it crucially creates an array of Boolean outputs - one per cell referenced in the range.
    It is this array of Booleans that permits us to essentially run the Evaluate over the range on a cell by cell basis - conducting an IF on each cell and acting accordingly (True,False).

    Without the ISTEXT the Evaluate will be conducted against the first cell in the range and result applied to entire range.

    More commonly you see

    Evaluate("IF(ROW(" & .Address & "),somefunction)")

    ie ROW is used to generate numeric values as the IF test (only 0 is FALSE of course) ... however here it makes sense to use an actual ISTEXT test given we want to process differently
    (ie we don't want to apply our formula to numeric cells etc)
    Last edited by DonkeyOte; 02-17-2010 at 02:51 PM. Reason: reworded

  9. #9
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848

    Re: Fix names with code

    Thanks for the explanation.

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Fix names with code

    No problem.

    A far better explanation of the ISTEXT/ROW approach can be seen at the following link:

    http://www.mrexcel.com/forum/showpos...0&postcount=13

    Kudos as always to the author, PGC01 - responsible for countless jaw dropping solutions.

+ 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