Writing array to a range
Peter
Thanks. I think I'm missing something as it runs but nothing is deposited in the range. My VBA skills aren't great and my understanding of arrays even worse.
This is what I ended up with ...
Sub DelimitedTextFileToArray()
Dim Delimiter As String
Dim TextFile As Integer
Dim FilePath As Variant 'String
Dim FileContent As String
Dim LineArray() As String
Dim DataArray() As Variant
Dim TempArray() As String
Dim rw As Long, col As Long
Dim filt_array As Variant
Dim dep_array As Variant
Application.StatusBar = False
FilePath = Application.GetOpenFilename(FileFilter:="Text File (*.txt),*.txt")
If FilePath = False Then
''''''''''''''''''''''''''
' user cancelled, get out
''''''''''''''''''''''''''
Exit Sub
End If
'Inputs
Delimiter = ","
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
'do nothing
ElseIf InStr(1, LineArray(x), "FF") Then
'Split up line of text by delimiter
TempArray = Split(LineArray(x), Delimiter)
'Determine how many columns are needed
col = UBound(TempArray)
If col maxcol Then
maxcol = col
'Re-Adjust Array boundaries
ReDim Preserve DataArray(UBound(LineArray), col)
End If
'Load line of data into Array variable
For y = LBound(TempArray) To UBound(TempArray)
DataArray(x, y) = TempArray(y)
'Debug.Print DataArray(1, rw)
Next y
'End If
End If
'Next line
rw = rw + 1
Next x
Dim Destination As Range
Set Destination = Range("a1")
Destination.Resize(rw, maxcol + 1).Value = DataArray
End Sub
|