从多个文本文件中提取一行数据并导入到Excel中

我有一个文件夹中的数百个文本文件,我需要从每个文件中提取一行,并将信息放入Excel中。 文本文件包含个别照片的所有元数据,我只需要取出GPS坐标。

我已经查看了各种其他类似的线程,例如: 从一个文件夹中的多个文本文件提取数据到Excel工作表

和:

http://www.mrexcel.com/forum/excel-questions/531515-visual-basic-applications-retrieve-data-text-file.html (对不起,不是stackoverflow!)

和其他许多人,但不能完全得到它的工作。 我很接近,但不是那里。

每个文本文件中的数据都是这样设置的:

---- Composite ---- Aperture : 3.8 GPS Altitude : 37.2 m Above Sea Level GPS Date/Time : 2014:05:15 10:30:55.7Z GPS Latitude : 50 deg 7' 33.40" N GPS Longitude : 5 deg 30' 4.06" W GPS Position : 50 deg 7' 33.40" N, 5 deg 30' 4.06" W Image Size : 4608x3456 

我写了下面的代码:

 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:\Users\Desktop\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 Loop Close #1 MyFile = Dir() posGPS = InStr(text, "GPS Position") nextrow = Sheet1.Cells(Rows.Count, "A").End(xlUp).row + 1 Sheet1.Cells(nextrow, "A").Value = Mid(text, posGPS + 33, 37) Loop End Sub 

它似乎打开每个文本文件,并通过它们来查看,但只提取从第一个文件的GPS坐标,并反复把这个在Excel中,所以我最终成百上千的行填充相同的数据 – GPS坐标从第一个文件在文件夹中。

如果任何人都可以帮助我完成这最后一点,将不胜感激!

谢谢

您必须重置您的text否则第二个文件的内容将被添加并且不会被replace,search将始终find第一个GPS数据并停止search:

 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