I have developed the below code to open a large number of text files (within the same folder) and copy everything from each file into excel (one cell for each line of text file & one row for each text file).
However, I do not require all of the data from the text files and it is slowing down the process. The text files are in following format:
DATASET UNSTRUCTURED_GRID
POINTS 5 float
0.096853 0.000000 0.111997
0.096853 -0.003500 0.111997
0.096890 0.000000 0.084015
0.096853 -0.003500 0.111997
0.096890 -0.003500 0.084015
CELL_DATA 5
SCALARS pressure float 1
LOOKUP_TABLE default
-0.000000
-0.000000
-3.000000
-2.000000
-6.000000
The data that I need to copy from this file is the second batch of numbers (below "LOOKUP_TABLE default"). The number of lines in this example is five (as stated on line starting "CELL_DATA" but this number can change from file to file.
In summary, I'm looking my code to only copy this last batch of numbers into excel instead of everything but I'm at a loss on how to tackle this.
Any help or advice would be greatly appreciated.
Sub ImportTextFile()
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As String
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Long
Dim SaveColNdx As Integer
FName = "E:\zdump\"
MyFile = Dir(FName & "*.txt")
Sep = vbLf
SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
Do While MyFile <> ""
Open (FName & MyFile) For Input As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
ColNdx = SaveColNdx
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
ColNdx = ColNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
Wend
RowNdx = RowNdx + 1
Wend
Close #1
MyFile = Dir()
Debug.Print text
Loop End Sub
Give this a try:
Dim RowNdx As Integer
Dim ColNdx As Integer
Dim TempVal As String
Dim WholeLine As String
Dim Pos As Integer
Dim NextPos As Long
Dim SaveColNdx As Integer
Dim SaveRowNdx As Long
Dim FoundData As Boolean
Dim NumberOfData As Long
FName = "E:\zdump\"
MyFile = Dir(FName & "*.txt")
Sep = vbLf
ColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row
SaveRowNdx = RowNdx
Do While MyFile <> ""
Open (FName & MyFile) For Input As #1
While Not EOF(1)
Line Input #1, WholeLine
If Right(WholeLine, 1) <> Sep Then
WholeLine = WholeLine & Sep
End If
Pos = 1
NextPos = InStr(Pos, WholeLine, Sep)
While NextPos >= 1
TempVal = Mid(WholeLine, Pos, NextPos - Pos)
If FoundData = False Then
If InStr(TempVal, "CELL_DATA") Then
NumberOfData = Val(Right(TempVal, Len(TempVal) - Len(Left(TempVal, Len("CELL_DATA") + 1))))
End If
If InStr(TempVal, "LOOKUP_TABLE default") <> 0 Then
FoundData = True
End If
Pos = NextPos + 1
NextPos = InStr(Pos, WholeLine, Sep)
Else
If NumberOfData <> 0 Then
Cells(RowNdx, ColNdx).Value = TempVal
Pos = NextPos + 1
RowNdx = RowNdx + 1
NextPos = InStr(Pos, WholeLine, Sep)
NumberOfData = NumberOfData - 1
End If
End If
Wend
Wend
Close #1
FoundData = False
ColNdx = ColNdx + 1
Cells(SaveRowNdx, ColNdx).Activate
RowNdx = SaveRowNdx
MyFile = Dir()
'Debug.Print Text
Loop
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.