I'm trying to use VBA to extract data from a series of text files. So far I've found the code below at The VBA Guide To Interacting With Text (.Txt) Files — The Spreadsheet Guru
But this takes my data and puts it into columns in my spreadsheet. I need it in rows so I've tried to change LineArray with ColArray but it hasn't worked. What should I do?
My data is in blocks separated by blank lines in the text file; do I need to allow for these blanks to make sure the array sees all?
Code:
Sub DelimitedTextFileToArray()
'PURPOSE: Load an Array variable with data from a delimited text file
'SOURCE: www.TheSpreadsheetGuru.com
Dim Delimiter As String
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim LineArray() As String
Dim DataArray() As String
Dim TempArray() As String
Dim rw As Long, col As Long
'Inputs
Delimiter = ";"
FilePath = "C:\Users\chris\Desktop\MyFile.txt"
rw = 0
'Open the text file in a Read State
TextFile = FreeFile
Open FilePath For Input As TextFile
'Store file content inside a variable
FileContent = Input(LOF(TextFile), TextFile)
'Close Text File
Close TextFile
'Separate Out lines of data
LineArray() = Split(FileContent, vbCrLf)
'Read Data into an Array Variable
For x = LBound(LineArray) To UBound(LineArray)
If Len(Trim(LineArray(x))) <> 0 Then
'Split up line of text by delimiter
TempArray = Split(LineArray(x), Delimiter)
'Determine how many columns are needed
col = UBound(TempArray)
'Re-Adjust Array boundaries
ReDim Preserve DataArray(col, rw)
'Load line of data into Array variable
For y = LBound(TempArray) To UBound(TempArray)
DataArray(y, rw) = TempArray(y)
Next y
End If
'Next line
rw = rw + 1
Next x
End Sub
But this takes my data and puts it into columns in my spreadsheet. I need it in rows so I've tried to change LineArray with ColArray but it hasn't worked. What should I do?
My data is in blocks separated by blank lines in the text file; do I need to allow for these blanks to make sure the array sees all?