+ Reply to Thread
Results 1 to 6 of 6

How to Separate Chunks of Characters and put them in separate Columns

  1. #1
    Registered User
    Join Date
    02-22-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    25

    How to Separate Chunks of Characters and put them in separate Columns

    Hello All, I need to create a program wherein I will be reading from a text file and then I'll print the characters in the Excel sheet. I can basically read and write to the excel sheet.

    But there are certain fields where the details must be put.Problem is, I have a lot of characters that are chunked together which makes it hard for me to determine which details goes into which field.

    To make it clear,
    In my Excel sheet I have 3 Fields namely,
    Column A = ID number (max of 8 characters)
    Column B = Name (max of 10 characters)
    Column C = Description (max of 20 Characters)

    If the maximum length of characters are not all used up, then I can put spaces in it.

    So in my text file that I will read, It has:

    10827835Crisshinn QWERTY

    wherein:
    10827835 = ID number
    Crisshinn = Name
    QWERTY = Description

    Notice that there is 1 space between "Crisshinn" and "QWERTY", this is the number of spaces that was printed in the textfile in order to complete all 10 characters.

    However, the "10827835" and "Crisshinn" part are chunked together. Since the ID number detail must always have max of 8 characters. Therefore, it will always be chunked together with the Name detail. So How can I separate them and display it in the 2 columns?

    I know I need to count the characters but I'm not too familiar on what function or how I should use it. Any help is greatly appreciated. Thank you :D
    Last edited by crisshinn; 03-13-2011 at 07:08 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: How to Separate Chunks of Characters and put them in separate Columns

    Will the ID always be 8 characters then?

    Dom
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: How to Separate Chunks of Characters and put them in separate Columns

    I've attached an example for either fixed or variable ID length. For a variable ID it uses a UDF to extract the numerical digits, assuming the only numbers in the string belong to the ID.

    Dom
    Attached Files Attached Files
    Last edited by Domski; 03-07-2011 at 10:08 AM.

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: How to Separate Chunks of Characters and put them in separate Columns

    Or maybe without VBa

    Assuming your data begins in A2

    Try this array formula in B2 (This will extract up to 10 digits from the string)
    Please Login or Register  to view this content.
    Confirm with Ctrl+Shift+Enter

    in C2
    Please Login or Register  to view this content.
    in D2
    Please Login or Register  to view this content.
    Drag/Fill all three down to suit

    Hope this helps
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  5. #5
    Registered User
    Join Date
    02-22-2011
    Location
    Philippines
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: How to Separate Chunks of Characters and put them in separate Columns

    Quote Originally Posted by Domski View Post
    Will the ID always be 8 characters then?

    Dom
    I'm sorry there's one part I got wrong. Actually, NO. The maximum number of digits for the ID is 12. So if all 12 characters are not used, then spaces must be printed to complete the 12characters. So sorry about that. Thats why I can't use your =Left(A2,8) because if I increase the range, it will also include the Characters.

    I am inclined to use the =ExtractDigits(A1) function although... I need to increment the Range so that I can loop it throughout the excel. So I can't really put A1 because it's too specific. I was thinking of using the line Cells(x,y) as the range, so that I can just increment x and y, but I can't combine it with the ExtractDigits function. ;(
    Last edited by crisshinn; 03-08-2011 at 10:17 AM.

  6. #6
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: How to Separate Chunks of Characters and put them in separate Columns

    As it's always 8 characters then the example in row 2 which doesn't need the VBA udf will suffice.

    For info though the function uses what are known as Regular Expressions to extract just the numerical digits from the string. I'll admit I don't really understand their full power myself and but have seen some very clever things done with them.

    Option Explicit is just an option in the VBA editor that requires you to declare any variables that you use within your code. There aren't any used in the code in the example, it's just a setting I always have on and would recommend everyone does. You can find some more info on it here.

    Dom

+ 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