Forum Discussion
learnerjane
Jul 15, 2022Copper Contributor
Excel VBA code to break and read text file line by line
Hello, I have a requirement. The below is a text file data. In this, I have to ignore the lines starting with HEAD and count the number of lines starting with DET for each data group. Eg....
- Jul 15, 2022
Here you go.
Sub ProcessTextFile() Dim sFile As String Dim f As Integer Dim sLines As String Dim aLines() As String Dim aLine() As String Dim i As Long Dim n As Long Dim sTitle As String ' *** Change sFile to the correct path and filename *** sFile = "Test.txt" f = FreeFile Open sFile For Input As #f sLines = Input(LOF(f), #f) Close #f aLines = Split(sLines, vbCrLf) For i = 0 To UBound(aLines) If aLines(i) <> "" Then aLine = Split(aLines(i), "|") Select Case aLine(0) Case "HEAD" If n > 0 Then Debug.Print "Group " & sTitle & " has " & n & " items" End If sTitle = aLine(1) n = 0 Case "DET" n = n + 1 End Select End If Next i Debug.Print "Group " & sTitle & " has " & n & " items" End Sub
HansVogelaar
Jul 15, 2022MVP
Here you go.
Sub ProcessTextFile()
Dim sFile As String
Dim f As Integer
Dim sLines As String
Dim aLines() As String
Dim aLine() As String
Dim i As Long
Dim n As Long
Dim sTitle As String
' *** Change sFile to the correct path and filename ***
sFile = "Test.txt"
f = FreeFile
Open sFile For Input As #f
sLines = Input(LOF(f), #f)
Close #f
aLines = Split(sLines, vbCrLf)
For i = 0 To UBound(aLines)
If aLines(i) <> "" Then
aLine = Split(aLines(i), "|")
Select Case aLine(0)
Case "HEAD"
If n > 0 Then
Debug.Print "Group " & sTitle & " has " & n & " items"
End If
sTitle = aLine(1)
n = 0
Case "DET"
n = n + 1
End Select
End If
Next i
Debug.Print "Group " & sTitle & " has " & n & " items"
End Sub
- learnerjaneJul 29, 2022Copper Contributor
HansVogelaar below is the code which I modified to print the output in excel columns. Tried various things but in vain. Could you please help in getting the output like the one in the attached image? Note that I'm selecting multiple text files to read.
Current output:
Issue: The file name in second row should be test-Copy.dat
Desired Output:
Code:
FileToOpen = Application.GetOpenFilename _ (Filefilter:="HDL Files (*.dat), *.dat", Title:="Select HDL Files", MultiSelect:=True) r = 2 If IsArray(FileToOpen) Then For FileCnt = 1 To UBound(FileToOpen) Filename = fso.getfilename(FileToOpen(FileCnt)) f = FreeFile Open FileToOpen(FileCnt) For Input As #f sLines = Input(LOF(f), #f) Close #f If InStr(sLines, vbCrLf) Then delim = vbCrLf ElseIf InStr(sLines, vbLf) Then delim = vbLf End If aLines = Split(sLines, delim) For i = 0 To UBound(aLines) If aLines(i) <> "" Then aLine = Split(aLines(i), "|") Select Case aLine(0) Case "HEAD" If n > 0 Then 'r = r + 1 Debug.Print "File Count: " & FileCnt Debug.Print r & "row" Debug.Print "In " & Filename & ", Group " & sTitle & " has " & n & " items" With shHDLRecordCountReport .Range("A" & r).Value = Filename .Range("B" & r).Value = sTitle .Range("C" & r).Value = n .UsedRange.Columns.AutoFit End With r = r + 1 End If sTitle = aLine(1) n = 0 Case "DET" n = n + 1 End Select End If Next i Debug.Print "Bottom" Debug.Print "File Count: " & FileCnt Debug.Print r & "row" Debug.Print "In " & Filename & ", Group " & sTitle & " has " & n & " items" With shHDLRecordCountReport .Range("A" & r).Value = Filename .Range("B" & r).Value = sTitle .Range("C" & r).Value = n .UsedRange.Columns.AutoFit End With Next FileCnt MsgBox "Report is ready", vbInformation, "Good Job" End If
- HansVogelaarJul 29, 2022MVP
Does this do what you want?
FileToOpen = Application.GetOpenFilename _ (Filefilter:="HDL Files (*.dat), *.dat", Title:="Select HDL Files", MultiSelect:=True) r = 1 If IsArray(FileToOpen) Then For FileCnt = 1 To UBound(FileToOpen) FileName = FSO.GetFileName(FileToOpen(FileCnt)) f = FreeFile Open FileToOpen(FileCnt) For Input As #f sLines = Input(LOF(f), #f) Close #f If InStr(sLines, vbCrLf) Then delim = vbCrLf ElseIf InStr(sLines, vbLf) Then delim = vbLf End If aLines = Split(sLines, delim) For i = 0 To UBound(aLines) If aLines(i) <> "" Then aLine = Split(aLines(i), "|") Select Case aLine(0) Case "HEAD" If n > 0 Then r = r + 1 Debug.Print "File Count: " & FileCnt Debug.Print r & "row" Debug.Print "In " & FileName & ", Group " & sTitle & " has " & n & " items" With shHDLRecordCountReport .Range("A" & r).Value = FileName .Range("B" & r).Value = sTitle .Range("C" & r).Value = n .UsedRange.Columns.AutoFit End With End If sTitle = aLine(1) n = 0 Case "DET" n = n + 1 End Select End If Next i r = r + 1 Debug.Print "Bottom" Debug.Print "File Count: " & FileCnt Debug.Print r & "row" Debug.Print "In " & FileName & ", Group " & sTitle & " has " & n & " items" With shHDLRecordCountReport .Range("A" & r).Value = FileName .Range("B" & r).Value = sTitle .Range("C" & r).Value = n .UsedRange.Columns.AutoFit End With n = 0 Next FileCnt MsgBox "Report is ready", vbInformation, "Good Job" End If
- learnerjaneJul 23, 2022Copper ContributorWorked like a charm Thanks a lot!