+ Reply to Thread
Results 1 to 7 of 7

VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

  1. #1
    Registered User
    Join Date
    07-12-2014
    Location
    North Carolina
    MS-Off Ver
    Office 2007
    Posts
    79

    VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    All,
    First of all I want to say Thankyou to everyone on this board that keeps helping me. The people in this forum really know their stuff- it's amazing.

    Now I have another question. I have a column that will always have a 5 digit number; the first 2 numbers represent a marketplace, and the last 3 numbers represent a location. I'm trying to build/find something that can split this 5 digit number into two cell (in separate columns) separated as listed above. I've attached a spreadsheet with a before and after, hopefully that will help explain better?

    Thanks in advance,

    Chad
    Attached Files Attached Files

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    This will give you what you need...
    =LEFT(B2,2)
    (or =LEFT(Before!B2,2) if you need it on another sheet)
    =RIGHT(B2,3)
    (or(=RIGHT(Before!B2,3)

    Note that you have a trailing space in B2. If that will be common amongst your data, change the 2nd formula to...
    =MID(B4,3,3)
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Registered User
    Join Date
    07-12-2014
    Location
    North Carolina
    MS-Off Ver
    Office 2007
    Posts
    79

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    That worked awesome. I have one more question though. I put the following code into the Macro I'm building based on it:
    Please Login or Register  to view this content.
    However- what do you think would be the best way to modify the code so that it will run until it reached the last row? (and not keep looping and making the Macro slower).

    Thanks again, what you provided already was a huge help.

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    If you have a list of values that you want to split, why not just use those as regular formulas if the columns?

    You could modify them to return nothing if B has nothing in it, something like...

    =IF(B2="","",LEFT(B2,2)

    Then you wouldnt need VBA

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

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    Quote Originally Posted by Chad Bateman View Post
    However- what do you think would be the best way to modify the code so that it will run until it reached the last row? (and not keep looping and making the Macro slower).
    Try
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    07-12-2014
    Location
    North Carolina
    MS-Off Ver
    Office 2007
    Posts
    79

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    That's perfect. Thanks Jindon.

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

    Re: VBA Code that can split the contents of a cell into two cells? (Spreadsheet Attached)

    No problem and thanks for the rep.

    If this is a one time operation, just TextToColumn with fixed width should do like the code does...

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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