+ Reply to Thread
Results 1 to 16 of 16

Replace multiple instances of spaces with a single delimiter

  1. #1
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Replace multiple instances of spaces with a single delimiter

    Hello there!

    I'm working on the beginning steps of a macro for a co-worker but first need to get the data in a format that I can work with. When the data comes into Excel (I'm using Excel 2010) it's all in 1 column instead of spread out over several columns like it should be. There are no tabs or commas used as delimiters; it's all separated by random numbers of spaces! So, I can't use delimiters in a Text-To-Columns. Of course each row of data might need to be spread across 2, 3, 4 or more columns, so I can't use fixed width, either.

    So, my next thought was to replace any instance of more than one space with a tab (or comma), and THEN do Text-To-Columns. But I can't figure out how to do that. I've been googling around and found something that looked promising, but it was from 9 years ago and it doesn't seem to want to work for me.
    What it says to do is: in "Find what", enter a space followed by {2,}, then in "Replace with" put in ^t. Here's the problem - next it wants me to check "Use wildcards" but I don't seem to have that option anywhere! (That's probably one more useful thing they eliminated when they "upgraded" Excel. Wish they'd leave stuff alone!)

    I suppose it's possible to create a macro without having to parse the data out across columns, but I can't imagine how that would go! I'll attempt to attach a workbook showing the raw data in its original form and how we want it to ultimately turn out.

    (If someone can just help me get the data parsed out correctly I THINK I can work through the rest of it over time. Unless there's some way to achieve the final desired result that skips the Text-To-Columns business)

    Thanks!

    Jenny
    Attached Files Attached Files
    Last edited by zookeepertx; 05-21-2018 at 05:00 PM.

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Replace multiple instances of spaces with a single delimiter

    Just make sure the Treat consecutive delimiters as one is checked.
    Attached Images Attached Images
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    I tried that, but it divides all the data up even when it's just separated by a single space.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Replace multiple instances of spaces with a single delimiter

    Quote Originally Posted by zookeepertx View Post
    I've been googling around and found something that looked promising, but it was from 9 years ago and it doesn't seem to want to work for me.
    What it says to do is: in "Find what", enter a space followed by {2,}, then in "Replace with" put in ^t. Here's the problem - next it wants me to check "Use wildcards" but I don't seem to have that option anywhere! (That's probably one more useful thing they eliminated when they "upgraded" Excel. Wish they'd leave stuff alone!)
    I suspect what you found was for Word- Excel has never had that ability, unless you write code to do Regex. I’ll post something later when I’m in the office.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Replace multiple instances of spaces with a single delimiter

    Something like this to process the current selection
    Please Login or Register  to view this content.
    You may wish to use
    Please Login or Register  to view this content.
    to avoid leading and trailing spaces being converted.

  6. #6
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    YES! That works beautifully; it's awesome! Thank you so much! (I changed the "bar" to a comma, just because comma or tab are the delimiters I'm used to seeing.)

    Now the NEW problem I've discovered is that there are more inconsistencies in the number of spaces within a line of data than I thought, so the text-to-columns is not always leaving things lined up correctly.

    I'll re-attach the file with a tab showing how it looks after the macro and Text-To-Columns.

    For example - In the first row of data (row 25):
    • There are multiple spaces between "LCS18" and "TWWJ3", so the macro is putting a comma there when it doesn't need one.
    • Also in the first row there's only 1 space between "TWWJ3" the size ("Mediu") so it's NOT getting a comma when it needs one.
    • In the first 2 rows there's only 1 space before "VS" so there's not a comma going in there when it needs one.
    • But, in the 3rd and 4th rows, there are multiple spaces before "VS" so the macro is correctly putting a comma in there.
    • In all of the rows, there's only 1 space after the digit after the color so those aren't getting a comma but they should

    I'm afraid this report might be exporting in such a ridiculously chaotic manner that it may be impossible to get it organized correctly without an act of God or something, LOL!
    Meanwhile, we're checking with the person that creates the report in the first place to see if it can be exported directly as a .csv; hopefully that would make it at least START OUT like a normal file.

    Anyway, thank you so much for your coding! I'm sure I'll still be able to use it, if not here, then in another project. (I haven't given up on this one yet, though).

    Have a great day!

    Jenny
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    08-17-2012
    Location
    Chicago, IL
    MS-Off Ver
    Office 365
    Posts
    35

    Re: Replace multiple instances of spaces with a single delimiter

    I copied your original data block into Notepad and changed the font to Courier. The incoming text is position aligned. You may want to try handling it as you would an incoming prn file or use the mid function to put each "section" of the string into a separate cell.

    See attached image.

    TextColumns.jpg

  8. #8
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    Hmmm, that's interesting! I wonder why changing it to Courier makes the whole layout change like that.

    I don't know anything at all about prn files; I'll have to Google that. I'm about to leave for the day, but I'll look at this some more tomorrow.

    Thanks for the ideas and I'll come back tomorrow and let you know if I got anywhere, LOL!

    Jenny

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Replace multiple instances of spaces with a single delimiter

    Courier is a fixed width font, which is why it lines up better.

    That should mean that you can use the fixed width option in Text to columns, at least for the ticketing information. It would still be far simpler to get a csv though!

  10. #10
    Registered User
    Join Date
    08-17-2012
    Location
    Chicago, IL
    MS-Off Ver
    Office 365
    Posts
    35

    Re: Replace multiple instances of spaces with a single delimiter

    I was in a rush yesterday or else I would have tried the Text to Columns using the "fixed width" option. Just tried it now, it works great in defining your columns. Good luck.

  11. #11
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    I had an epiphany this morning!

    It seems that each row has the same number of available spots - 131. Each block of information has a set number of available spots, whether they're all filled or not. So, if the name location has 30 spots available and their name is 20 characters long, there will be 10 spaces in there to fill the rest of the spots. If the person's name is only 10 characters long, there will be 20 spaces after it to fill in all the spots!

    So, on the Ticket Number section, the "Ticket Number" starts as character 1, "METHOD" starts at character 15, "AIRBILL" starts at character 22, etc.

    Now I just need to figure out how to get the macro to go to cell A1, get characters 1-13 and put them where they belong. Then go to A1 again, get characters 15-20 and put them where they belong, get characters 22-36 and put them where they belong, etc. If I can get that going & reorganize all the data from 1 order #, then start over again with the next order #, maybe it'll go along okay.

    I have hope again! Just gotta figure out the syntax for all that!
    Last edited by zookeepertx; 05-24-2018 at 09:55 AM.

  12. #12
    Registered User
    Join Date
    08-17-2012
    Location
    Chicago, IL
    MS-Off Ver
    Office 365
    Posts
    35

    Re: Replace multiple instances of spaces with a single delimiter

    Isn't it wonderful when the light bulb comes on!

    I do this quite frequently with text files. I'm not saying my method is the best one but it's been working for me.

    Here's how you would extract the pieces of the string and assign them to the new cell locations:

    Please Login or Register  to view this content.
    Instead of "value" you can also use "text" but I believe you have prices and sales etc that would need the value option.
    You may want to format each cell at the same time you are populating it.

  13. #13
    Registered User
    Join Date
    08-17-2012
    Location
    Chicago, IL
    MS-Off Ver
    Office 365
    Posts
    35

    Re: Replace multiple instances of spaces with a single delimiter

    One more thing I forgot to mention. If you don't want those trailing blanks to be in your new cell location, precede the "Left" or "Mid" functions with the "Trim" function.

  14. #14
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    That looks like it's going to work for that problem very nicely!

    But, in scrolling through the 5,000+ row report, I've noted 1 or 2 other anomalies -

    First:The 2 blocks containing "Name, Address, City, State, Zip, Phone #" is not organized the same on every single record. There only appear to be 2 formats, though.
    There's a "Sold To" block and a "Ship To" block.
    • On some records, the "Sold To" block is on 1 set of rows and the "Ship To" block is a couple of rows below it. (See the top part of Sheet1 on the attachment)
    • On other records, the 2 blocks are distributed across single rows. (See the next part of Sheet1 on the attachment)

    Either way they start out, they need to turn out organized like what you see on Sheet2 on the attachment.

    NOTE: Sheet1 has a couple of rows that are blue and are numbered in sequence - left to right. There are 131 available spots on the report, so each of the cells represent an available "spot" on the report. All 131 "spots" load into column A on the report when it's exported into Excel. So, basically, the 131 columns on Sheet1 represent what's in column A when the report downloads. Any cells on Sheet1 that are blank are actually spaces to be dealt with when parsing out the data.
    NOTE2: Sheet2 has its top row pink. That just represents the number of the column where the data needs to get moved to, NOT spaces.
    (I just did this to help me visualize the spaces, etc.

    I know how I'd deal with the problem when the blocks are on different rows, but when there are 2 names (or addresses - whatever) in the same cell, with a LOT of spaces separating them and 2 addresses in the same cell, with a LOT of spaces separating them, etc I just can't picture how to get them to do what I want!

    SECOND:The city, state and zip each need to end up in their own cells. If that particular block is alone in the cell, I can do that with Left, Mid, Right. But if there's a City, State, Zip in a cell, then a lot of spaces, then another City, State, Zip - well, I can't seem to picture how to do that. The same problem exists in the "phone number" row.

    I'm sorry; I know this is really long, but I want to give you all the info up front.
    So far, no epiphanies today, LOL! Maybe you have one?

    Thanks!

    Jenny
    Attached Files Attached Files

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

    Re: Replace multiple instances of spaces with a single delimiter

    This creates new sheet and output the half processed data from "Raw data-sample" sheet in your post #6.
    Then you can do a TextToColumns with a caret ^ and hope make it easier.
    Please Login or Register  to view this content.

  16. #16
    Forum Contributor
    Join Date
    06-25-2013
    Location
    Longview, TX
    MS-Off Ver
    MS365
    Posts
    338

    Re: Replace multiple instances of spaces with a single delimiter

    Quote Originally Posted by jindon View Post
    This creates new sheet and output the half processed data from "Raw data-sample" sheet in your post #6.
    Then you can do a TextToColumns with a caret ^ and hope make it easier.
    Thank you for that coding! However, because the number of spaces between blocks of text varies from row to row, a text-to-columns doesn't seem to be an option. Sometimes, there's only 1 space between pieces of text that actually need to have a delimiter but the macro doesn't assign a delimiter, since there's only 1 space. And sometimes, there are multiple spaces between words that shouldn't have a delimiter but a macro assigns one, since there's more than one space. (The original report was formatted by a crazy person, evidently, LOL!)

    Thanks for your time spent on the problem.

    Jenny

+ 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] Remove consecutive spaces and replace with single comma
    By maacmaac in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-11-2015, 05:20 PM
  2. [SOLVED] import text file using multiple spaces as delimiter
    By Kaden265 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-03-2014, 11:50 AM
  3. [SOLVED] Calling multiple instances of a single userform
    By Sorjas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-29-2014, 08:47 AM
  4. Replace white spaces with tab delimiter to creare table
    By thadacto in forum Word Programming / VBA / Macros
    Replies: 4
    Last Post: 12-01-2013, 03:56 PM
  5. Multiple Instances to Single Instance
    By jcholla in forum Excel General
    Replies: 2
    Last Post: 11-16-2011, 04:22 PM
  6. Reading text files into excel with multiple spaces as delimiter
    By XLBob in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2009, 10:05 AM
  7. Find Multiple instances of Single Criterion in Row & Return To a Single Col
    By Sam via OfficeKB.com in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 05-09-2006, 10:10 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