I am trying to write a VBA script to extract information from a text document and tabulate it into corresponding columns. The code is based on https://stackoverflow.com/questions/51635537/extract-data-from-text-file-into-excel/51636080. I am having an issue doing multiple extractions.
Sample text
Age: 35
Rank: Lieutenant
Classification: Volunteer
Incident date: Jun 22, 1997
Date of death: Jun 22, 1997
Cause of death: Caught or Trapped
Nature of death: Burns
Activity type: Advance Hose Lines/Fire Attack (includes Wildland)
Emergency duty: Yes
Duty type: On-Scene Fire
Fixed property use: Residential
Memorial fund information:
Age: 18
Rank: Firefighter
Classification: Volunteer
Incident date: Jun 16, 1997
Date of death: Jun 17, 1997
Cause of death: Struck By
Nature of death: Trauma
Activity type: Driving/Operating Vehicle/Apparatus
Emergency duty: Yes
Duty type: Responding
Fixed property use: N/A
Memorial fund information:
Problem: VBA code fails after column "F" and does not move to the next row
Working code:
Sub ExtractData()
Dim filename As String, nextrow As Long, MyFolder As String
Dim MyFile As String, text As String, textline As String, filedate As String
Dim filenum As Integer
Dim idx%
MyFolder = "/Users/user/Downloads/test/"
MyFile = Dir(MyFolder & "*.txt")
nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do While MyFile <> ""
Open (MyFolder & MyFile) For Input As #1
'nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do Until EOF(1)
Line Input #1, textline 'read a line
idx = InStr(textline, "Age:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "A").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Rank:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "B").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Classification:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "C").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Incident date:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "D").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Date of death:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "E").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Cause of death:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "F").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
nextrow = nextrow + 1 'now move to next row
End If
Loop
Close #1
MyFile = Dir()
Loop
End Sub
Code that fails
Sub ExtractData()
Dim filename As String, nextrow As Long, MyFolder As String
Dim MyFile As String, text As String, textline As String, filedate As String
Dim filenum As Integer
Dim idx%
MyFolder = "/Users/josephheaton/Downloads/test/"
MyFile = Dir(MyFolder & "*.txt")
nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do While MyFile <> ""
Open (MyFolder & MyFile) For Input As #1
'nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Do Until EOF(1)
Line Input #1, textline 'read a line
idx = InStr(textline, "Age:") ' if has date, set it but not move to the next ROW
If idx > 0 Then
ActiveSheet.Cells(nextrow, "A").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Rank:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "B").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Classification:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "C").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Incident date:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "D").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Date of death:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "E").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Cause of death:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "F").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Nature of death:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "G").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Activity:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "H").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Emergency:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "I").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Duty:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "J").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Property type:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "L").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
End If
idx = InStr(textline, "Memorial fund info:")
If idx > 0 Then
ActiveSheet.Cells(nextrow, "L").Value = Right(textline, Len(textline) - InStr(textline, ":") - 1)
nextrow = nextrow + 1 'now move to next row
End If
Loop
Close #1
MyFile = Dir()
Loop
End Sub