Consulting

Results 1 to 5 of 5

Thread: Extracting data from a variable length string

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Extracting data from a variable length string

    Suppose you have the following:

    smith - xx - john
    anderson - xx - april
    williams - xx - john

    What i want to do is to have it end up like

    xx john smith
    xx april anderson
    xx john williams

    You cant use LEFT or MID or RIGHT afaik because of the length of the string.

    Anyone have any suggestions on how to accomplish this?

    The string would be in A1, A2, etc.
    Peace of mind is found in some of the strangest places.

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    ok so i got this far:

    anderson - xx - april

    using
    =LEFT(A1, FIND("-",A1)-1 )

    i can get Anderson

    how do you get April in the next adjoining cell?
    Peace of mind is found in some of the strangest places.

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Function Recombine(strIn As String)AS String
    'Assumes strIn is a 5 part string like "smith - xx - john"
    'Returns a 3 part string like "xx john smith"
    
    Dim Tmp
        tmp = Split(strIn, "-")
        Recombine = Trim(Tmp(1) & tmp(2) & tmp(0))
    End Function
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,874
    try:
    =MID(A1,FIND(" - ",A1)+3,FIND(" - ",A1,FIND(" - ",A1)+1)-FIND(" - ",A1)-3)
    to get the xx

    =MID(A1,FIND(" - ",A1,FIND(" - ",A1)+1)+3,999)
    to get the john

    =LEFT(A1, FIND(" - ",A1)-1 )
    to get the smith.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Quote Originally Posted by p45cal View Post
    try:
    =MID(A1,FIND(" - ",A1)+3,FIND(" - ",A1,FIND(" - ",A1)+1)-FIND(" - ",A1)-3)
    to get the xx

    =MID(A1,FIND(" - ",A1,FIND(" - ",A1)+1)+3,999)
    to get the john

    =LEFT(A1, FIND(" - ",A1)-1 )
    to get the smith.

    Thanks guys. Both work well.
    Peace of mind is found in some of the strangest places.

Posting Permissions

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