VBA to write into an array

JST013

Board Regular
Joined
Mar 11, 2015
Messages
74
Hello friends of mrexcel!


I am currently trying to wrap my head around arrays!

I have data written into cells on a worksheet. Each cell holds "rows" and "columns" I would like to write into an array. The "rows" are separated by "/" and the columns are separated by ",".

An example of a cell....

Apple,Banana,Orange,Grape/Carrot,Celery,Tomato,Eggplant/Simon,Lily,Peter,Abby/

I want to form an array using this data...I believe I need to use split in combination with two for loops...but I'm having a lot of trouble setting it up...The desired array would look like...


Apple Banana Orange Grape
Carrot Celery Tomato Eggplant
Simon Lily Peter Abby

What I thought I needed to do is not even close to working...
Here's my code...

Code:
thedata = ws.range("A1")
splitrows = Split(thedata,"/")
splitcolumns = Split(splitrows,",")
for i = Lbound(splitrows) to Ubound(splitrows)
for j = 1 to 4
[INDENT]splitcolumns = ws.cells(i+1,j)[/INDENT]
next

could anyone point me in the right direction?
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Sub Splitter(rg As Range)
Dim rws As Variant
Dim cols As Variant
rws = Split(rg.Value, "/")
For i = 0 To UBound(rws, 1)
cols = Split(rws(i), ",")
For j = 0 To UBound(cols, 1)
Cells(i + 1, j + 1).Value = cols(j)
Next
Next
End Sub

Then run:
Splitter Range("A1")
 
Upvote 0
Maybe:
Code:
Sub Test()
    Application.ScreenUpdating = False
    Dim myArray As Variant
    Dim i As Long
    myArray = Split(Range("A1"), "/")
    For i = LBound(myArray) To UBound(myArray)
        Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = myArray(i)
    Next i
    Application.ScreenUpdating = True
End Sub
Data will be written to column B.
 
Upvote 0
First of all, thank you both for your quick responses!

@BobUmlas special thanks because it works perfectly! Thank you
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top