Soo ive found a lot of similar questions but nothing that really fits what im looking to do, and im a little bit stuck.
Basically what im looking to do is have a cell (in this instance, A1), that has multiple values separated by commas (always 4 values), and then have it split into separate rows along columns.
Example
A1[10,9,8,6]
Needs to Become
a10[10], b10[9], c10[8], d10[6]
The current code im working with is :
Dim X As Variant
X = Split(Range("A1").Value, ",")
Range("a10").Resize(UBound(X) - LBound(X) + 1).Value = Application.Transpose(X)
This outputs the data vertically down a column though, when i need it to be outputted as i have shown above
You're resizing Range("A10")
in to a column range.
The Resize
method takes two arguments, both optional:
.Resize(_rows_, _columns_)
You have only supplied the first argument, which resizes A10 to a specified number of rows . For example, Debug.Print Range("A10").Resize(10).Address
should give you: $A$10:$A$19
, when what you want is $A$10:$J$10
.
Do this instead:
Range("a10").Resize(1, UBound(X) - LBound(X) + 1).Value = X
Also you're removing the call to Application.Transpose
, since the array is already in a row, you don't need to transpose it (previously you had to transpose it from a row, to a column).
ALSO the result of the Split function is always a 0-based array, even if you have Option Base 1
. So you can simply do:
Range("a10").Resize(1, UBound(X) + 1).Value = X
Alternatively, omit the first argument but include a comma to indicate that you're only passing the second argument:
Range("a10").Resize(, UBound(X) + 1).Value = X
The other method as Pieter suggests below would be to record (and modify) a macro using the built-in Text-to-Columns functionality. I don't think this will work for your case, because you're taking values from one row and putting them in another row. The built-in text-to-columns only outputs to the same row, so a custom split like you have created is the way to go.
Select the column containing the CSV values (Column A it seems in your example) and then from the Data Ribbon Data Tools group launch the **Text to Columns wizard. Specify your column s bein delimited with comma as the delimiter and watch the wizard do it's thing.
In order to program this operation in VBA, run it manually while recording a macro and then inspect and edit the macro.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.