Help with VBA run-time error when filling an array with comma delimited text file

cvrband

Board Regular
Joined
Jan 6, 2016
Messages
63
Office Version
  1. 365
Platform
  1. Windows
I have a comma delimited text file that I'm trying to place into an array so that I can call the array in a CAD program and place the contents as text. I thought I found the answer to filling the array on this webpage: www.thespreadsheetguru.com (see this section near the bottom: 'VBA For Fill Array With Delimited Data From Text File') but I keep getting a "Run-time error '9': Subscript out of range" error on the 2nd round of the For Next loop. Using the watch window, LineArray(), TempArray() and DataArray() are populated properly until the error. From what I've read researching this error, it seems to be a resizing issue but I can't figure out why. Can someone please help me fix this code or provide another solution? Thank you in advance.

Here is the code and sample text file below:
VBA Code:
Sub DelimitedTextFileToArray()

Dim Delimiter As String, FileContent As String
Dim r As Long, c As Long, i As Long, j As Long, TextFile As Integer
Dim DataArray() As String, TempArray() As String, LineArray() As String

  Delimiter = ","
  r = 0

  TextFile = FreeFile
    Open FullTxtPath For Input As TextFile   ' the variable 'FullTxtPath' is declared and set in earlier code
      FileContent = Input(LOF(TextFile), TextFile)
    Close TextFile
 
  LineArray() = Split(FileContent, vbCrLf)
 
  For i = LBound(LineArray) To UBound(LineArray)
          
        TempArray = Split(LineArray(i), Delimiter)
      'Determine how many columns are needed
            c = UBound(TempArray)
      'Re-Adjust Array boundaries
        ReDim Preserve DataArray(r, c)   '<=== This line causes the run-time error '9' on 2nd round of for/next loop
      'Load line of data into Array variable
        For j = LBound(TempArray) To UBound(TempArray)
          DataArray(r, j) = TempArray(j)
        Next j
            
      r = r + 1
      
  Next i
 
End Sub

Note that the number of lines will vary from project to project as will the line lengths with the first line always 1 less than the following lines.

SampleTxtFile.png
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
I have a comma delimited text file that I'm trying to place into an array so that I can call the array in a CAD program and place the contents as text. I thought I found the answer to filling the array on this webpage: www.thespreadsheetguru.com (see this section near the bottom: 'VBA For Fill Array With Delimited Data From Text File') but I keep getting a "Run-time error '9': Subscript out of range" error on the 2nd round of the For Next loop. Using the watch window, LineArray(), TempArray() and DataArray() are populated properly until the error. From what I've read researching this error, it seems to be a resizing issue but I can't figure out why. Can someone please help me fix this code or provide another solution? Thank you in advance.

Here is the code and sample text file below:
VBA Code:
Sub DelimitedTextFileToArray()

Dim Delimiter As String, FileContent As String
Dim r As Long, c As Long, i As Long, j As Long, TextFile As Integer
Dim DataArray() As String, TempArray() As String, LineArray() As String

  Delimiter = ","
  r = 0

  TextFile = FreeFile
    Open FullTxtPath For Input As TextFile   ' the variable 'FullTxtPath' is declared and set in earlier code
      FileContent = Input(LOF(TextFile), TextFile)
    Close TextFile
 
  LineArray() = Split(FileContent, vbCrLf)
 
  For i = LBound(LineArray) To UBound(LineArray)
        
        TempArray = Split(LineArray(i), Delimiter)
      'Determine how many columns are needed
            c = UBound(TempArray)
      'Re-Adjust Array boundaries
        ReDim Preserve DataArray(r, c)   '<=== This line causes the run-time error '9' on 2nd round of for/next loop
      'Load line of data into Array variable
        For j = LBound(TempArray) To UBound(TempArray)
          DataArray(r, j) = TempArray(j)
        Next j
          
      r = r + 1
    
  Next i
 
End Sub

Note that the number of lines will vary from project to project as will the line lengths with the first line always 1 less than the following lines.

View attachment 53963
When doing redim preserve you can only change the last dimension of an array.
VBA Code:
Sub DelimitedTextFileToArray()
Dim FileContent As String
Dim r As Long, c As Long, i As Long, j As Long, TextFile As Integer
Dim DataArray() As String, TempArray() As String, LineArray() As String
  const Delimiter as string = ","
     r = 0
    TextFile = FreeFile
    Open FullTxtPath For Input As TextFile   ' the variable 'FullTxtPath' is declared and set in earlier code
      FileContent = Input(LOF(TextFile), TextFile)
    Close TextFile
 
    LineArray = Split(FileContent, vbCrLf)
   
    redim DataArray(ubound(LineArray),0)
    For i = LBound(LineArray) To UBound(LineArray)
         
        TempArray = Split(LineArray(i), Delimiter)
        'Re-Adjust Array boundaries
        if UBound(TempArray)> ubound(DataArray,2) Then ReDim Preserve DataArray(ubound(DataArray,1), UBound(TempArray))   '<=== This line causes the run-time error '9' on 2nd round of for/next loop
        'Load line of data into Array variable
        For j = LBound(TempArray) To UBound(TempArray)
            DataArray(i, j) = TempArray(j)
        Next j
           
        'r = r + 1
     
    Next i
 
End Sub
 
Upvote 0
Solution
Hello @MoshiM -

I'll have to study this a bit as I'm not proficient in arrays but the code revision you provided worked!

Thank you!!
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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