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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

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
11,721
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,874
Messages
5,834,170
Members
430,262
Latest member
JWDeiley

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