+ Reply to Thread
Results 1 to 11 of 11

Fixing delimiters

  1. #1
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Fixing delimiters

    I have a formula that is grabbing data from lots of places and putting semicolons between the data. Sometimes, the data comes with semicolons already in place. Sometimes, the data is empty, and I don't want any semicolons.

    What I wind up with is a string that sometimes starts with one or more semicolons. When this happens, I want to remove the starting semicolons (I want to start the string at the first non-semicolon character).
    Sometimes, the string ends with a semicolon. When this happens, I want to end the string with the last non-semicolon value.
    Sometimes, the string will contain multiple semicolons between two substrings (example: Bob;;Dole). When this happens, I want to replace the middle semicolons with a single semicolon.

    Much of the spreadsheet is locked, so I would like to do this in a single cell, if possible.

    Let's say the current formula is
    =FORMULA

    I was thinking of something like this to trim the initial and ending semicolons:
    =SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(FORMULA," ",CHAR(1)),";"," "))," ",";"),CHAR(1)," ")

    This takes care of all of the initial and ending semicolons, but not the duplicates in the middle. Is there a simple formula to take care of them?

    This gets me close:
    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(FORMULA," ",CHAR(1)),";"," "))," ",";"),CHAR(1)," "),";;",";")

    But, if there are more than 2 consecutive semicolons, like Bob;;;Dole, it only reduces the number of semicolons by 1.
    Last edited by SlipEternal; 02-28-2018 at 04:30 PM. Reason: Marking topic solved. Turns out there was no issue. I had already solved it and did not realize.

  2. #2
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Fixing delimiters

    Will you please attach a SMALL sample Excel workbook (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution is also shown (mock up the results manually).

    3. Make sure that all confidential information is removed first!!

    4. Try to avoid using merged cells. They cause lots of problems!

    Unfortunately the attachment icon doesn't work at the moment. So, to attach an Excel file you have to do the following: Just before posting, scroll down to Go Advanced and then scroll down to Manage Attachments. Now follow the instructions at the top of that screen.
    Glenn




    None of us get paid for helping you... we do this for fun. So DON'T FORGET to say "Thank You" to all who have freely given some of their time to help YOU.

    Temporary addition of accented to illustrate ongoing problem to the TT: L? fh?ile P?draig sona dhaoibh

  3. #3
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Fixing delimiters

    What's the maximum number of consecutive semicolons?

    Edit: This seems to work as desired....


    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(IF(LEFT(A1,1)=";",MID(IF(RIGHT(A1,1)=";",LEFT(A1,LEN(A1)-1),A1),2,LEN(A1)-2),IF(RIGHT(A1,1)=";",LEFT(A1,LEN(A1)-1),A1)),";;;;",";"),";;;",";"),";;",";")
    Attached Files Attached Files
    Last edited by Glenn Kennedy; 02-28-2018 at 01:57 PM.

  4. #4
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Fixing delimiters

    Attached is a sample with exactly 6 possible semicolons getting added. This means that getting rid of leading or terminating semicolons, I only need to worry about at most 4 "inner" semicolons. So, I made it so that it can handle up to 4 "inner" semicolons. But, as soon as I increase the number of mantles it can handle, it adds the possibility of a 5th internal semicolon that my code will not handle.
    Attached Files Attached Files

  5. #5
    Forum Moderator Glenn Kennedy's Avatar
    Join Date
    07-08-2012
    Location
    Digital Nomad... occasionally based in Ireland.
    MS-Off Ver
    O365 (PC) V 2403
    Posts
    44,063

    Re: Fixing delimiters

    I didn't see any ;; in your "sample"

  6. #6
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Fixing delimiters

    Quote Originally Posted by Glenn Kennedy View Post
    I didn't see any ;; in your "sample"
    Correct. I gave my solution to the problem that works for up to four consecutive ;'s. I can probably figure it out. I was just hoping someone had a formula for turning multiple repeated characters into a single character where there was no limit to the number of repeated characters.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Fixing delimiters

    Can't you replace all instance of ";" with single space. Then use trim and substitute back ";"?

    =SUBSTITUTE(TRIM(SUBSTITUTE(A2,";"," "))," ",";")

    Edit: Oh, wait, you got spaces in your original string. You'll need additional step. Replace initial space character with some non-existent text such as "@".
    So...
    =SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(A2," ","@"),";"," "))," ",";"),"@"," ")
    Last edited by CK76; 02-28-2018 at 03:26 PM.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  8. #8
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Fixing delimiters

    Quote Originally Posted by CK76 View Post
    Can't you replace all instance of ";" with single space. Then use trim and substitute back ";"?

    =SUBSTITUTE(TRIM(SUBSTITUTE(A2,";"," "))," ",";")

    Edit: Oh, wait, you got spaces in your original string. You'll need additional step. Replace initial space character with some non-existent text such as "@".
    So...
    =SUBSTITUTE(SUBSTITUTE(TRIM(SUBSTITUTE(SUBSTITUTE(A2," ","@"),";"," "))," ",";"),"@"," ")
    Yes, my original post has almost this exact formula. I used CHAR(1) rather than "@". This gets rid of leading and trailing semicolons. The issue is now getting rid of the duplicate middle semicolons. So, I was hoping for a formula that would take any number of middle semicolons and turn it into just one semicolon. Using repeated SUBSTITUTE functions, I can make a specific number of repeated semicolons go to one semicolon, but not an arbitrarily large number.

  9. #9
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Fixing delimiters

    Hmm? The formula should work for however many number of semicolons you have repeated.

    1. It first replaces existing " " single space with "@" (or any non-existent character).
    2. Then replaces ALL instance of ";" with " " single space.
    3. Use trim to get rid of preceding and trailing space, and keep only single space in between.
    4. Replace single space with ";".
    5. Replace "@" back to single space.

    See attached.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    07-28-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    104

    Re: Fixing delimiters

    Quote Originally Posted by CK76 View Post
    Hmm? The formula should work for however many number of semicolons you have repeated.

    1. It first replaces existing " " single space with "@" (or any non-existent character).
    2. Then replaces ALL instance of ";" with " " single space.
    3. Use trim to get rid of preceding and trailing space, and keep only single space in between.
    4. Replace single space with ";".
    5. Replace "@" back to single space.

    See attached.
    Oh, awesome. I was just working through the logic. I had not realized that my formula actually worked! You are correct. It takes care of all of the extra semicolons.

    Thanks!

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Fixing delimiters

    You are welcome and thanks for the rep

+ 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. vlookup with delimiters
    By karthik013 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-29-2014, 11:28 AM
  2. [SOLVED] Extracting a Substring Between Second and Third Delimiters
    By MrGadget6977 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-18-2013, 05:54 PM
  3. various runs of spaces as delimiters
    By Excelvator in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 08-24-2012, 11:05 PM
  4. Referencing Delimiters
    By Impartial Derivative in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2011, 12:07 AM
  5. concatenate and delimiters redux
    By goji in forum Excel General
    Replies: 5
    Last Post: 09-21-2009, 03:27 PM
  6. concatenating w/ delimiters
    By goji in forum Excel General
    Replies: 3
    Last Post: 09-21-2009, 02:34 PM
  7. Replies: 2
    Last Post: 08-01-2006, 08:50 PM
  8. Extract based on Delimiters
    By ssjody in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-26-2005, 11:30 AM

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