+ Reply to Thread
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: Parse string into 3 parts using vba

  1. #1
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Parse string into 3 parts using vba

    I a column of strings such as below:

    AAAAAAAAAAAAABBBBBBBBBB (229Dec2009) fkjdfldjkflskjflaskfjlsjdflasjlfjslfjksjflkasjdflkj (+/-7 days) jdlfkjslfjlsdfjk (14Oct2009). Please clarify.' canceled (This stuff).

    I want to parse the string such that
    String1: ' canceled
    String2: This stuff
    String3: AAAAAAAAAAAAABBBBBBBBBB (229Dec2009) fkjdfldjkflskjflaskfjlsjdflasjlfjslfjksjflkasjdflkj (+/-7 days) jdlfkjslfjlsdfjk (14Oct2009). Please clarify.

    Any ideas?
    Last edited by welchs101; 07-07-2011 at 10:42 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Parse string into 3 parts using vba

    hi, welchs101, you'd better posted sample workbook showing original data and result you need to obtain

  3. #3
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    i am enclosing an example of what i want. if you need clarification on anything just let me know. thanks again.
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Parse string into 3 parts using vba

    Are there all possible options for strings syntax shown?
    Last edited by watersev; 06-28-2011 at 09:22 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Parse string into 3 parts using vba

    please check attachment, run code "test", result is on Sheet3
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    wow.........that was fast. I am looking at your code now.....have some questions but i will try to ask them later once i have fully reviewed the code. thanks.

  7. #7
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    i am looking up some of your code and i am having trouble finding good explanations for the following..........was wondering if you could help me figure these out.

    x(i, 2) = Trim(Split(x(i, 1), "' canceled")(0))
    x(i, 3) = Trim(Replace(Split(Replace(x(i, 1), x(i, 2), ""), "(")(1), ")", ""))
    So, i am guessing that the first one "removes" canceled from the string.........but not sure what the "(0)" is doing?

  8. #8
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Parse string into 3 parts using vba

    when split function is used the first element of the returned array of strings is zero
    So if we apply this function to "zero, one, two" string, element (0) of split with delimiter "," will give you "zero". See VB help for Split function.
    Replace method obiously replaces some old symbol(s) to new once ("(" to "" and ")" to "").
    Trim function deletes any extra spaces.

  9. #9
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    got pulled away from this topic but now i am back on it again.

    I had a question about the code you posted:

    x = .Range(.[a2], .Cells(Rows.Count, "a").End(xlUp).Offset(, 2))
    can you explain this a little.

    I know your making an array assignment but i am having a hard time understanding what is exactly getting assigned.

  10. #10
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: Parse string into 3 parts using vba

    define area in A column and offset to the right by 2 columns

  11. #11
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    I am still going through some of the code........i think i can learn some things i dont already know.

    Some of my requirements have changed. i no longer need to parse the string into 3 components as previously stated but rather two .

    I want to parse the string such that
    String1: This stuff
    String2: AAAAAAAAAAAAABBBBBBBBBB (229Dec2009) fkjdfldjkflskjflaskfjlsjdflasjlfjslfjksjflkasjdflkj (+/-7 days) jdlfkjslfjlsdfjk (14Oct2009). Please clarify.' canceled

    Basically, now i need to parse the string into two segments.

    I can parse the string into two segment using what i have learned here but i cant seem to get the "canceled" part intot the first string.

    Any ideas?

  12. #12
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    in an attempt to understand the code and satisfy my current requirements i am have this code but i am getting a type mismatch run time error .......cant figure out why



    Dim junk2 As Variant
    junk1 = "AAAAAAAAAAAAABBBBBBBBBB (229Dec2009) fkjdfldjkflskjflaskfjlsjdflasjlfjslfjksjflkasjdflkj (+/-7 days) jdlfkjslfjlsdfjk (14Oct2009). Please clarify.' canceled (This stuff)."
    junk2 = Split(junk1, "' canceled")(0)
    MsgBox (junk2 & "' canceled")
    MsgBox (junk2(1, 1))

    error occurs on the "MsgBox (junk2(1, 1))" part of the code....i think

  13. #13
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: Parse string into 3 parts using vba

    In your code junk2 becomes a single string - text prior to delimiter.

    Using your specific approach above you would modify to:

    junk2 = Split(junk1,"' canceled") '2 item 0-based array
    MsgBox junk2(0) & "' canceled"
    MsgBox junk2(1)
    obviously modify per your own requirements.

  14. #14
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    which line makes junk2 a single string?

    is it this?

    junk2 = Split(junk1, "' canceled")(0)

    what does this do exactly?

    Is junk2 still an array even if i use the "(0)"

    Also, your code works.........thanks.

    just trying to understand why.

  15. #15
    Forum Guru
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2007
    Posts
    1,252

    Re: Parse string into 3 parts using vba

    also what if the "(0)" had been a "(1)" what would this do or mean?

+ 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.2.0