+ Reply to Thread
Results 1 to 7 of 7

function or vba to Split numbers from one cell

  1. #1
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    function or vba to Split numbers from one cell

    I am importing a xml file. The resulting file has one range of cells which contain multiple numbers.

    numbers are in the format xx-xx-xx-xx for example 12-1-2-4.

    I need to break the bunbers up to individual cells.

    so if A1 = 12-1-2-4
    it becomes

    B1 = 12
    C1 = 1
    D1 = 2
    E1 = 4

    Is this doable?

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: function or vba to Split numbers from one cell

    Hi Flebber, this macro should work for you. It assumes your xml data is in A1:A? on Sheet1. Adjust the code as needed to suit.

    Please Login or Register  to view this content.
    Hope that helps!

  3. #3
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: function or vba to Split numbers from one cell

    Thanks for the solution. I would like to understand it a bit so I can learn from it. How exactly is this line working?

    Please Login or Register  to view this content.

  4. #4
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,885

    Re: function or vba to Split numbers from one cell

    Ok, where to begin.. this is going to get a bit technical, so hopefully you can follow along.

    Please Login or Register  to view this content.
    That line will loop through the array named "arr" from the first entry to the last. In the line before that, I did a mass-move of data from A1:A?? into a two-dimensional horizontal array [arr(1,1) being the first value in that array, arr(2, 1) being the second, etc]. UBound stands for Upper Bound.

    Please Login or Register  to view this content.
    From the previous line, we're now looping through the array item by item starting at 1. So when i = 1, this line of code is essentially
    Please Login or Register  to view this content.
    arr(1, 1) refers to the first entry in the array, for example, "12-1-2-4" from cell A1.
    Using the Split function splits that one entry into, in this case, four separate entries using "-" as the delimeter. So instead of having one array entry with "12-1-2-4", I now have four separate values in a temporary array, consisting of 12, 1, 2 and 4. Since I've referred to 4 individual cells to the left of the = sign (B1:E1), I am simply assigning those four cells the four values of the temporarily split array value.

    Because the array is horizontal (it's created that way when you first load the values into the array even though it came from values going down a column), when you paste the array back to the worksheet you can paste it directly into a horizontal range containing the same number of cells as in the array (or temporary array in this case). If I had wanted to return the values to the worksheet in one column (e.g. B1:B4), I would need to Transpose the array (change it from horizontal to vertical) by using
    Please Login or Register  to view this content.
    After that first line is done, we loop and i changes to 2. So we're then looking at the second value in the original array, as well as referring to the second row on the worksheet. Had the worksheet data started in a different row, like 20, we could have adjusted the code slightly to take that into account when adding the newly split data back to the sheet.

    Hopefully that helps in some way!

  5. #5
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: function or vba to Split numbers from one cell

    you could have used text to columns with -as delimiter
    either manually or something like just adapted from a recorded macro
    Please Login or Register  to view this content.
    Last edited by martindwilson; 07-18-2010 at 07:03 AM.
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

  6. #6
    Forum Contributor
    Join Date
    07-16-2009
    Location
    australia
    MS-Off Ver
    Excel 2007
    Posts
    116

    Re: function or vba to Split numbers from one cell

    I have small issues with script that I struggling to resolve. Main issue is that first column where all values start by the end of the operation all values still remain. I was hoping that the only value remaining in the originating cell would be the dollar value.. Because the first cell contains its values for example as
    Please Login or Register  to view this content.
    Other small issue is that the last delimited value when copied to its new column also copies the dollar value with it as well.

    Thinking of using a cut and paste method. Thought is to cut all delimited values out of cell and paste in first column and then distribute values from last number.

    So 1-2-3-4 gets cut from 4 first and leaves first value in cell.
    The only method I can seems to find is pastespecial but not sure.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: function or vba to Split numbers from one cell

    you can still used text to columns
    but fiddle it a bit
    this macro copies col a to col b
    text to columns on col a but skips all fields except last so dollar amount text remains in col a. then repeats on column b but skips the dollar amount
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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