+ Reply to Thread
Results 1 to 19 of 19

Split text avoiding field separator within double quotes

  1. #1
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Split text avoiding field separator within double quotes

    I have a text containing strings separated by comma. But some strings have inside commas, then to differentiate, these
    strings are surrounded by double quotes like below:

    commas in red are those that separate each strings.
    Please Login or Register  to view this content.
    I'm trying with code below, but is not working since is splitting MyText in 9 strings, when I want to get only 7.

    How can I split string by commas without consider the commas inside the double quotes?

    Please Login or Register  to view this content.
    The output desired should be

    Please Login or Register  to view this content.
    Thanks in advance for any help

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Split text avoiding field separator within double quotes

    I occassionally use the following technique which requires additional work:
    a. Replace the commas inside the double quotes with a character or sequence of characters that is known not to exist in your string (e.g. '|', or '@', or 'QQQ').
    b. Run the split command
    c. Use the 'replace()' function on each array member to restore the original commas.

    Lewis

  3. #3
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Split text avoiding field separator within double quotes

    Here is a macro that looks awful and all brute force but it works.
    Please Login or Register  to view this content.
    Pierre Leclerc
    _______________________________________________________

    If you like the help you got,
    Click on the STAR "Add reputation" icon at the bottom.

  4. #4
    Forum Contributor
    Join Date
    04-24-2012
    Location
    Karlstad, Sweden
    MS-Off Ver
    Excel 2016
    Posts
    232

    Re: Split text avoiding field separator within double quotes

    I see p24leclerc has given you a code that works. I will attach mine also.

    Result is stored in asResult().

    Please Login or Register  to view this content.
    1. Reply to thread and inform if suggestion was helpful or not
    2. Click on the star (=Add Reputation) if you think someone helped you
    3. Mark [SOLVED] to this thread if solution was found. (On Menu "Thread Tools" > "Mark this thread as solved")

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Split text avoiding field separator within double quotes

    Hello cgkmal,

    This macro uses a more advanced method to parse the text . It uses Regular Expressions.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Split text avoiding field separator within double quotes

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-06-2014
    Location
    Center, Eastland
    MS-Off Ver
    Excel 365
    Posts
    11

    Re: Split text avoiding field separator within double quotes

    Woow!

    Hello all of you great contributors, I didn't expect such number of suggestions and solutions for my question.

    I've tested all of them and work just fine. I'll learn from every solution shared

    Many thanks for your help people.

    Regards

  8. #8
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: Split text avoiding field separator within double quotes

    Hi Jindon,

    Your solution is very short and really impressive. Could you please elaborate the pattern you have used in provided solution.

    Regards,

  9. #9
    Forum Expert p24leclerc's Avatar
    Join Date
    07-05-2010
    Location
    Québec
    MS-Off Ver
    Excel 2021
    Posts
    2,081

    Re: Split text avoiding field separator within double quotes

    JINDON's pattern = [^"",]+|""[^""]+""

    the first part looks for the ordinary coma separated characters
    [^"",] character class looking for anything being a comma or not a double quote ("")
    + means one or more times.

    | an OR statement

    The second part looks for the QUOTE enclosed characters
    "" It has to start with a double quote
    [^""]+ it can contains any characters but a double quote one or more times (+)
    "" It ends with a double quote

    ^ is the exclusion symbol in RegEx
    ""[^""]+""

    Leith Ross' pattern = ""[^""]+""|\w+(?=\,)

    He starts looking for the Quote enclosed characters
    ""[^""]+"" It starts with a double quote, contains any characters but a double quote, one or more times and ends with a double quote.
    | the OR symbol
    \w+ looks for any characters one or more times
    (?=\,) looks ahead for a comma to end those characters.

    That is why Leith has to concatenate a comma at the end of the search string (in order to get the last word)

    Hoping this helps you understand those patterns
    If I'm wrong, I'm sure jindon or Leith Ross will correct me.
    Regards

  10. #10
    Forum Contributor
    Join Date
    08-17-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    132

    Re: Split text avoiding field separator within double quotes

    Thanks for your detailed explanation...

  11. #11
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Split text avoiding field separator within double quotes

    Hello guys again,

    Thank you for the great solutions provided. I have an issue when string has empty values. I mean, where there are 2 or more commas consecutives.

    for example if the string is like this:
    Please Login or Register  to view this content.
    When is splitted should be nine elements, but with the current code only appears 7. I've tried to add toyour regex "|\,\," but it seems is not correct.

    May you help me again to fix this?

    Thanks in advance

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Split text avoiding field separator within double quotes

    Please Login or Register  to view this content.

  13. #13
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Split text avoiding field separator within double quotes

    Hello jindon,

    Thanks for your answer.

    Is not working. The following string, str = a,"f,2",4,,5,,,,"k,4",,g has 11 fields but the regex is seeing only 6.

    The current VBA code looks like this.
    Please Login or Register  to view this content.
    Output now
    w(0) -> a
    w(1) -> "f,2"
    w(2) -> 4
    w(3) -> 5
    w(4) -> "k,4"
    w(5) -> g

    Desired output
    w(0) -> a
    w(1) -> "f,2"
    w(2) -> 4
    w(3) ->
    w(4) -> 5
    w(5) ->
    w(6) ->
    w(7) ->
    w(8) -> "k,4"
    w(9) ->
    w(10) -> g

    Thanks in advance for any help.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Split text avoiding field separator within double quotes

    Please Login or Register  to view this content.

  15. #15
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Split text avoiding field separator within double quotes

    Hello jindon,

    It almost work for all cases. Is not working if MyText = "f oui, 2",a,"f,2",4,,5,,,,"k,4",,g

    Regarding that, I don't understand some things, maybe you have some time to explain me please.

    What it is ".test()"? is a property of "CreateObject("VBScript.RegExp")"?
    I understand that .Execute() is when you say the Pattern to work on. Work on (MyText). But what does it mean the (0)?

    How it is that the pattern contains the Chr(2) and you execute the regex before to replace the commas by Chr(2) in MyText?

    May you explain how the Regex you used work.

    Thanks again

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Split text avoiding field separator within double quotes

    MyText = "f oui, 2",a,"f,2",4,,5,,,,"k,4",,g

    Doesn't even compile.

    I will leave this to someone else as I don't understand what you are trying to do.

  17. #17
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Split text avoiding field separator within double quotes

    Hello jindon,

    Only try to split separated by commas, but without confusing the commas that are inside the fieds surrounded by double quotes.

    The text doesn't compile since I showed as it is, to compile I need to write it inside the code like this:

    Please Login or Register  to view this content.
    The first field is "f oui, 2" but is being splitted in 2 fields in the output.

    Thanks for all the help.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,592

    Re: Split text avoiding field separator within double quotes

    Please Login or Register  to view this content.

  19. #19
    Forum Contributor
    Join Date
    06-07-2011
    Location
    Hnd
    MS-Off Ver
    Excel 2010
    Posts
    161

    Re: Split text avoiding field separator within double quotes

    Thank you jindon,

    With that regex works correctly now!

    Regards

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Double Quotes using Chr(34) and Extra Quotes when saving as text file
    By vamosj in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-26-2013, 12:26 PM
  2. Avoiding double-count of cells with similar text
    By MJCharaf in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 01-10-2013, 11:23 AM
  3. export .CSV with field containing double quotes?
    By William DeLeo in forum Excel General
    Replies: 5
    Last Post: 04-05-2006, 01:08 PM
  4. Add double quotes to each field
    By npereira in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-12-2005, 02:15 PM
  5. Replies: 4
    Last Post: 08-10-2005, 10:05 PM

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