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?
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.
Hope that helps!Option Explicit Sub flebber() Dim arr As Variant, i As Long With Sheets("Sheet1") arr = .Range("A1:A" & .UsedRange.Rows.Count) For i = 1 To UBound(arr) .Range("B" & i & ":E" & i).Value = Split(arr(i, 1), "-") Next i End With End Sub
Thanks for the solution. I would like to understand it a bit so I can learn from it. How exactly is this line working?
For i = 1 To UBound(arr) .Range("B" & i & ":E" & i).Value = Split(arr(i, 1), "-")
Ok, where to begin.. this is going to get a bit technical, so hopefully you can follow along.
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.For i = 1 To UBound(arr)
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.Range("B" & i & ":E" & i).Value = Split(arr(i, 1), "-")
arr(1, 1) refers to the first entry in the array, for example, "12-1-2-4" from cell A1..Range("B1:E1").Value = Split(arr(1, 1), "-")
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
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..Range("B1:B4").Value = Application.Transpose(Split(arr(i, 1), "-"))
Hopefully that helps in some way!
you could have used text to columns with -as delimiter
either manually or something like just adapted from a recorded macro
Sub t2c() Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ Other:=True, OtherChar _ :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)) End Sub
Last edited by martindwilson; 07-18-2010 at 07:03 AM.
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
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
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
Other small issue is that the last delimited value when copied to its new column also copies the dollar value with it as well.21-7-4-1 $20000.00
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.
expression.PasteSpecial(Paste, Operation, SkipBlanks, Transpose)
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
Sub t2c() Application.ScreenUpdating = False Range("b:b").Value = Range("A:A").Value Columns("A:A").TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _ Space:=True, Other:=True, OtherChar _ :="-", FieldInfo:=Array(Array(1, 9), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 1)) Columns("b:b").TextToColumns Destination:=Range("b1"), DataType:=xlDelimited, _ Space:=True, Other:=True, OtherChar _ :="-", FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 9)) Application.ScreenUpdating = True End Sub
"Unless otherwise stated all my comments are directed at OP"
Mojito connoisseur and a dabbler in Cisco
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks