Splitting into multi-dimensional array

kpark91

Well-known Member
Joined
Jul 15, 2010
Messages
1,582
Hello,
I need to be able to split a string into multi-dimensional array.

At first, I opened a file delimited by semicolon.
and I just want to read first 4 lines of the file to grab the data I need.

Here is an attempt which failed:
Code:
Sub Test()
    Dim objFSO As New FileSystemObject, objFile As Object, i&, strDataSetting$(0 To 3, 0 To 5)
 
    If objFSO.FileExists(strPath) Then
        With objFSO.OpenTextFile(strPath, ForReading)
            For i = 0 To 3
                If Not .AtEndOfStream Then
                    strDataSetting(i,) = Split(.ReadLine, ";")
                End If
            Next i
        End With
    End If
 
End Sub

so, my array would look something like
Code:
strDataSetting
i\j  0    1    2    3    4    5
0    a    b    c    d    e    f 
1    a    b    c    d    e    f
2    a    b    c    d    e    f
3    a    b    c    d    e    f

at the end of the day, if the file looks like this:

a;b;c;d;e;f
a;b;c;d;e;f
a;b;c;d;e;f
a;b;c;d;e;f


Thank you in advance,
Kpark.

Any suggestions are welcome!

PS: If anybody is wondering why I might be using a 2-dimensional array is because I need to make sure the file is valid.
That means the file must consist of at least 4 lines but I had no way of checking it without reading the whole file first then looping to find the number of vbNewLine.
So, in this, I'm trying to store the data while I am reading each line of the file which should give better runtime.
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi

If, like in this case, you have a dynamic array, you cannot set the dimension bounds beforehand. In this case you are generating the array for each line using Split() and so you'll get a jagged array.

Try:

Code:
Sub Test()
Dim objFSO As New FileSystemObject, objFile As Object
Dim i As Long, strDataSetting(0 To 3) As Variant, strPath As String
 
strPath = "c:\tmp\Test.txt"
 
If objFSO.FileExists(strPath) Then
    With objFSO.OpenTextFile(strPath, ForReading)
        For i = 0 To 3
            If Not .AtEndOfStream Then
                strDataSetting(i) = Split(.ReadLine, ";")
            End If
        Next i
        .Close
    End With
End If
 
' display the second character in the third line
MsgBox strDataSetting(2)(1)
 
End Sub

I used a text file with:

a;b;c;d;e;f
g;h;i;j;k;l
m;n;o;p;q;r
s;t;u;v;w;x
 
Upvote 0
Hello, thank you for the reply.

the code is giving a compile error stating "Wrong number of dimensions"
at
Code:
MsgBox strDataSetting(2)(1)
 
Upvote 0
Something must be wrong with your declaration of strDataSetting.

Please copy the code I posted and change only the pathname of the file.
 
Upvote 0
Found out why.
Rich (BB code):
With objFSO.OpenTextFile(strPath, ForReading, False, TristateTrue)

is the correct way of opening UTF text files.
 
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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