Results 1 to 5 of 5

Thread: Split Function That Ignores Delimiters Located Inside Quote Marks

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13

    Split Function That Ignores Delimiters Located Inside Quote Marks

    NOTE
    -------
    I have updated the code in this message on Sept. 21, 2012 to reflect an excellent observation by snb in the messages he posted to this thread. Thanks snb!

    Earlier today, Rasm started a thread (VBA - Split command) where he asked for the ability to use the Split function, but have it not split text at delimiters inside quote marks. An example would be this comma-delimited text string...

    One,Two,"Three,Four",Five

    where the returned array (call it Arr for this example) should be this...

    Arr(0) ==> One
    Arr(1) ==> Two
    Arr(2) ==> "Three,Four"
    Arr(3) ==> Five

    Of course, VBA's built-in Split function does not do this... it will split apart the text in Arr(2) blindly ignoring the quote marks. Now, while this question has only just been asked in this forum, it is any old problem having roots going way back. Many, many years ago back when I was volunteering answering questions on the old Compiled VB Newsgroups, I posted a function that did what Rasm has asked for. Below is that function, but to which I have added some additional functionality.

    Code:
    Function QuotedSplit(ByVal TextToSplit As String, _
                         Optional Delimiter As String = ",", _
                         Optional RemoveDuplicates As Boolean = False, _
                         Optional Compare As VbCompareMethod = vbBinaryCompare) As String()
      Dim X As Long, QuoteDelimited() As String, WorkingArray() As String
      QuoteDelimited = Split(TextToSplit, """")
      For X = 0 To UBound(QuoteDelimited) Step 2
        QuoteDelimited(X) = Replace$(QuoteDelimited(X), Delimiter, Chr$(1), , , Compare)
      Next
      TextToSplit = Join(QuoteDelimited, """")
      If RemoveDuplicates Then
        Do While InStr(1, TextToSplit, Chr(1) & Chr(1), Compare)
          TextToSplit = Replace$(TextToSplit, Chr(1) & Chr(1), Chr(1), , , Compare)
        Loop
      End If
      WorkingArray = Split(TextToSplit, Chr$(1))
      QuotedSplit = WorkingArray
    End Function
    The above function has four arguments... the first is the text that you want to split apart and is, of course, required; the second, the text you want to use as the delimiter is optional and is defaulted to a comma, but which can be any text string composed of one or more characters; the third is a Boolean which allows you to specify how consecutive delimiters are to be treated, where the default is False meaning the each delimited field will be returned even if that delimited field is the empty string ("") and where True will make the function treat consecutive delimiters as if they were a single delimiter; and the fourth is an optional built-in data type (VbCompareMethod - actually, a restricted Long underneath it all) which, if the delimiter has alphabetic characters in it, allows you to make the split case sensitive or not... the default is vbBinaryCompare (0) meaning the text must match the case of the delimiter you pass in exactly, set it to vbTextCompare (1) if you want the split to be performed with regard for the letter casing of the delimiter.

    Oh, and I guess I should mention... this function, like the Split function which it is modeled after, returns a zero-based one-dimensional String array. Okay, that is it... try playing around with the various options so you can get a "feel" for what they do and how they interact with each other.
    Last edited by Rick Rothstein; 09-21-2012 at 10:26 PM.

Similar Threads

  1. UDF (user defined function) replacement for Excel's DATEDIF function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 21
    Last Post: 03-07-2015, 09:47 PM
  2. Split Text with Delimiter
    By jaffey in forum Excel Help
    Replies: 1
    Last Post: 06-07-2013, 02:25 PM
  3. Replies: 5
    Last Post: 05-28-2013, 03:00 AM
  4. Split data
    By jan in forum Excel Help
    Replies: 19
    Last Post: 08-12-2012, 02:50 AM
  5. Split Cell and Lookup
    By bsiq in forum Excel Help
    Replies: 4
    Last Post: 11-21-2011, 10:03 PM

Tags for this Thread

Posting Permissions

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