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?
 

Some videos you may like

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

BobUmlas

Well-known Member
Joined
Mar 14, 2002
Messages
1,181
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")
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
10,121
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.
 

JST013

Board Regular
Joined
Mar 11, 2015
Messages
74
First of all, thank you both for your quick responses!

@BobUmlas special thanks because it works perfectly! Thank you
 

Watch MrExcel Video

Forum statistics

Threads
1,122,669
Messages
5,597,474
Members
414,145
Latest member
lonnie451

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
Top