+ Reply to Thread
Results 1 to 23 of 23

Split Number To Digits

  1. #1
    Registered User
    Join Date
    04-11-2007
    Posts
    49

    Split Number To Digits

    Can a vba macro be provided for splitting a number into digits? The number will be in Sheet 1 but splitted number will be on sheet 2. Splitting of numbers means a number entered into a cell will be splitted into different column/cells with one digit per cell.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    I think this will do what you want.

    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Another option

    Please Login or Register  to view this content.

    rylo

  4. #4
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Thanks to duo for the solution but how can I modify the code for more than one row involved? secondly, this code has to be run for getting the solution . can the code be made in such a manner that as soon as data in specified cells of sheet1 are entered, number will be splitted automatically?

  5. #5
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Change it to an event macro. Right click on the sheet tab where you are going to enter the number, select view code, and paste the following code.

    Please Login or Register  to view this content.
    Adjust so that the output sheet is correct.

    Now enter a number in column1 of the source sheet, and it should split the output to the output sheet.


    rylo

  6. #6
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Thanks for the reply But where can I make the chages for start column in output sheet. In output sheet splitting is starting from column A, but if I want the same to start from column D, where I have to make changes?

    Secondly, Say in cell A1 of input sheet is of 5 digits and cell A2 of input sheet is of 6 digits, in Output sheet, number alingnment is not correct.ie output is coming as below:

    12345 is 1 2 3 4 5
    123456 is 1 2 3 4 5 6

    whereas I want
    12345 is 1 2 3 4 5
    123456 is 1 2 3 4 5 6

  7. #7
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    To start in Col D change this line

    Please Login or Register  to view this content.
    to

    Please Login or Register  to view this content.
    both the below look the same to me. So can't help
    12345 is 1 2 3 4 5
    123456 is 1 2 3 4 5 6

    whereas I want
    12345 is 1 2 3 4 5
    123456 is 1 2 3 4 5 6
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  8. #8
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Thanks for the reply regarding Output display sorry that it did not display correctly. I am attaching a sample WB as display is not proper here.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Someone please have a look into it.

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    How are we to know the maximum number of digits that you want to format so it can be right justified? Is 6 the absolute limit for every number??? What happens if a number is input with more than 6 digits?


    rylo

  11. #11
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Thanks for the reply. Maximun length of the number will be 14 User cannot enter more than 14 digit number in input sheet as validation will restrict them.

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    HI

    See how this goes.

    Please Login or Register  to view this content.

    rylo

  13. #13
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Exactly working as per desired manner. One guidance
    1. Just tell me the logic where can I change the column start no. In your code, in output sheet, it is starting from Column N as I told it will be for 14 digits. But in case I need for say 10 digits, 6 digits etc., where shall I make the changes? So that I can use the code in future also with modification.

    One problem in the code:
    1. In your code, in the input sheet, if the number contains zero, in output sheet, that cell remains blank. say the no. is 1001 in the inout sheet, in the output sheet, the result is 1 <blank> <blank> 1. Why it is not recognizing zeros?

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    Why it is not recognizing zeros?
    A slight mod to Rylo's code changes this:
    Please Login or Register  to view this content.
    Just tell me the logic where can I change the column start no.
    Change the value of constant n.

  15. #15
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    HI

    Yea, didn't consider that.

    How about

    Please Login or Register  to view this content.
    2) Modify the number in the line
    Please Login or Register  to view this content.
    to determine the number of characters you want to see.


    rylo

  16. #16
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    How about ajusting the first column by the Len of the number.
    Please Login or Register  to view this content.

  17. #17
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    How about ajusting the first column by the Len of the number.
    Valid alternative, Mike, if right-justifying the results is not significant.

  18. #18
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    The first digit is offset (14-Len(holder))+1 columns
    The last digit is offset (14-Len(holder))+Len(holder) = 14 columns

    That is right justified, as sujittalukder wants.

  19. #19
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    My apology, Mike, I did not read your code carefully.

  20. #20
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    No problem. You just incited the explaination that I should have included at first.

  21. #21
    Registered User
    Join Date
    04-11-2007
    Posts
    49
    Thanks to all for the reply. The code is working perfectly.

  22. #22
    Registered User
    Join Date
    02-02-2012
    Location
    philippines
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Split Number To Digits

    Amazing, but can you revise vb to include two decimal places. say the numbers 2550.00 and 550.01 and 50.00
    I tried the above but in 2550.00 the digits picked up were 2 5 5 0 but when i tried 550.01 result is 5 5 0 . 0 1
    Can I have a fix on this. Thank you

  23. #23
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Split Number To Digits

    To include two decimal places in this 5 year old thread ( ), change this line
    Please Login or Register  to view this content.
    Untested, but I think the 14 becomes 17.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

+ 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