I have a text file that I am trying to parse into Excel. The file fields are delimited by |. When I use the code below, it parses everything in to the first column. Can anyone help with how to modify the code so that it parses each line of the text file into the appropriate columns and rows?
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
Dim x As Integer
Dim y As Integer
'Inputs
Delimiter = "|"
FilePath = Application.GetOpenFilename
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
'Debug.Print DataArray(y, rw)
Range("Harp_Anchor").Offset(y, rw) = DataArray(y, rw)
Next y
End If
'Next line
rw = rw + 1
Next x
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
Dim x As Integer
Dim y As Integer
'Inputs
Delimiter = "|"
FilePath = Application.GetOpenFilename
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
'Debug.Print DataArray(y, rw)
Range("Harp_Anchor").Offset(y, rw) = DataArray(y, rw)
Next y
End If
'Next line
rw = rw + 1
Next x