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:
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.
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.