+ Reply to Thread
Results 1 to 13 of 13

VBA to break text up in single cell

  1. #1
    Registered User
    Join Date
    03-13-2016
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    10

    VBA to break text up in single cell

    I have the following cells of data. Each cell has data that I needed broken out into single cells. However, my customer can have the following variations: CUSTOMER, CUSTOMER ONE, CUSTOMER TWO. I would normally use Text-to-Columns, however since my customer could have a space, it messes up some cells.

    CUSTOMER 1369749 HARVEYT $0 ODate 7/7/2017
    CUSTOMER ONE 1369592 BUEHLC $96 ODate 7/6/2017
    CUSTOMER ONE TWO 1369592 BUEHLC $96 ODate 7/6/2017

  2. #2
    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: VBA to break text up in single cell

    Hello talmrubin,

    Welcome to the Forum!

    1) How many digits will the number have after the customer's name?

    2) Will the amount allows be an even dollar amount like $96 ?

    3) Will the date always be formatted as ODate m/d/yyyy ?
    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!)

  3. #3
    Registered User
    Join Date
    03-13-2016
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    10

    Re: VBA to break text up in single cell

    Leith
    Thanks for taking a stab, please see below"

    1) How many digits will the number have after the customer's name?
    7 digits

    2) Will the amount allows be an even dollar amount like $96 ?
    No, It could go out to the hundreths, so 96.25

    3) Will the date always be formatted as ODate m/d/yyyy ?
    It will actually be ODATE: m/d/yyy (I added in the colon)

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

    Re: VBA to break text up in single cell

    You can accomplish this with bit of formula & Text to Columns.

    1. Assuming data in A2:A4 range. Enter following formula in B2, copy down. This will extract string occurring before first numeric.
    =TRIM(LEFT(A2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A2&56^7))-1))

    2. In C2 following formula. Copy down.
    =SUBSTITUTE(A2,B2&" ","")

    3. Copy B2:C4 range and paste as value, and split Column C using Text to Column.

    4. You can then delete Column A, or keep it as is.
    Last edited by CK76; 07-10-2017 at 02:11 PM. Reason: Edit: for clarity.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

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

    Re: VBA to break text up in single cell

    For VBA, you can try below (it will replace column A values).

    Please Login or Register  to view this content.
    See attached demo.
    Attached Files Attached Files

  6. #6
    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: VBA to break text up in single cell

    Hello talmrubin,

    This macro will validated each of the fields and place each filed in it's own column on the right next to the original cell. The data is assumed to start in Cell "A1". You can change this in the macro if you need to.

    Copy the code below and paste it into a new VBA Module in your workbook. You can run the macro by using the ALT+F8 keys to display the Macro Dialog box.

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,882

    Re: VBA to break text up in single cell

    Thanks a lot Mr. Leith for your great efforts
    As for your code I think that "HARVEYT" part is missing from results ...
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  8. #8
    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: VBA to break text up in single cell

    Hello Yasser,

    Thanks for catching my mistake. Right you are. I missed adding a grouping in the Regular Expression. Here is the corrected version...
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    03-13-2016
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    10

    Re: VBA to break text up in single cell

    The above works well. What if I removed the 'ODate'? Can you help me adjust the code above?

    CUSTOMER 1369749 HARVEYT $0 7/7/2017
    CUSTOMER ONE 1369592 BUEHLC $96 7/6/2017
    CUSTOMER ONE TWO 1369592 BUEHLC $96 7/6/2017

  10. #10
    Registered User
    Join Date
    03-13-2016
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    10

    Re: VBA to break text up in single cell

    Quote Originally Posted by CK76 View Post
    For VBA, you can try below (it will replace column A values).

    Please Login or Register  to view this content.
    See attached demo.
    The above works well. What if I removed the 'ODate'? Can you help me adjust the code above?

    CUSTOMER 1369749 HARVEYT $0 7/7/2017
    CUSTOMER ONE 1369592 BUEHLC $96 7/6/2017
    CUSTOMER ONE TWO 1369592 BUEHLC $96 7/6/2017

  11. #11
    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: VBA to break text up in single cell

    Hello talmrubin,

    This will remove the Odate from the output.

    Please Login or Register  to view this content.

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

    Re: VBA to break text up in single cell

    Try
    Please Login or Register  to view this content.

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

    Re: VBA to break text up in single cell

    The above works well. What if I removed the 'ODate'? Can you help me adjust the code above?
    Here you go. Change "Range("B2").Resize..." to "Range("A2").Resize to replace original strings or keep it as is.
    Please Login or Register  to view this content.
    Last edited by CK76; 07-11-2017 at 08:44 AM. Reason: Fixed Typo

+ 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. Replies: 5
    Last Post: 11-05-2014, 01:49 AM
  2. Break apart a single cell to create a mailing list
    By mooresc in forum Excel General
    Replies: 2
    Last Post: 09-17-2014, 11:35 AM
  3. Macro to break out a single cell
    By lorber123 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-05-2013, 06:26 PM
  4. Replies: 7
    Last Post: 01-24-2013, 06:55 PM
  5. Separate out a text string in one cell, and break into into individual cells
    By StephenHall in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-05-2012, 03:45 PM
  6. Break-Up and Move Text To Next Cell
    By gpgreen in forum Excel General
    Replies: 10
    Last Post: 04-25-2009, 09:16 PM
  7. break line in text cell from ASP
    By Lorenzo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-06-2006, 09:10 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