Extract a single line of data from numerous text files and import into Excel

后端 未结 1 1441
太阳男子
太阳男子 2020-12-07 03:42

I have hundreds of text files in a folder and I need to extract a single line from each one and put the info into excel. The text files contain all the metadata for individu

相关标签:
1条回答
  • 2020-12-07 04:15

    You have to reset your text otherwise the content of the second file is added and not replaced and the search always find the first GPS data and stop searching:

    Sub ExtractGPS()
        Dim filename As String, nextrow As Long, MyFolder As String
        Dim MyFile As String, text As String, textline As String, posGPS As String
    
        MyFolder = "C:\Temp\Test\"
        MyFile = Dir(MyFolder & "*.txt")
    
        Do While MyFile <> ""
            Open (MyFolder & MyFile) For Input As #1
            Do Until EOF(1)
                Line Input #1, textline
                text = text & textline 'second loop text is already stored -> see reset text
            Loop
            Close #1
            MyFile = Dir()
            Debug.Print text
            posGPS = InStr(text, "GPS Position")
            nextrow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
            ActiveSheet.Cells(nextrow, "A").Value = Mid(text, posGPS + 33, 37)
            text = "" 'reset text
        Loop
    End Sub
    
    0 讨论(0)
提交回复
热议问题