我有一个文本文件,其中包含有关错误列表的数据。
我只想搜索在文件中多次出现的ERROR字,然后复制ERROR =后面的'......'中引用的数据 格式为:ERROR =“错误的简称。(单词数量可能有所不同,但是错误后面的文字用'...'引起来)”。
我尝试过打开文件:
Sub FileOpenDialogBox()
Dim myFile As String, text As String, textline As String
Dim posLat As Integer, posLong As Integer
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", " *.txt", 1
.Show
fullpath = .SelectedItems.Item(1)
End With
End Sub
并搜索和复制数据:
Sub import()
Dim myFile As String, text As String, textline As String
Dim posLat As Integer, posLong As Integer
myFile = "fullpath"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
posLat = InStr(text, "ERROR")
Range("A1").Value = Mid(text, posLat + 10, 5)
End Sub
使用此脚本,我无法从该文本文件复制每个错误,也无法捕获'......'之间的数据。
您能帮我吗?
答案 0 :(得分:0)
UPDATE
尝试
Sub import()
Dim myFile As String, text As String, textline As String
Dim posLat As Integer, posLong As Integer, i As Long
myFile = fullpath
Open myFile For Input As #1
i = 1
Do Until EOF(1)
Line Input #1, textline
posLat = InStr(textline, "ERROR = '") + 9
posLatFin = InStr(posLat, textline, "'")
Cells(1, i).Value = Mid(textline, posLat, posLatFin - posLat)
i = i + 1
Loop
Close #1
End Sub
OLD
尝试这样
Sub import()
Dim myFile As String, text As String, textline As String
Dim posLat As Integer, posLong As Integer, i as long
myFile = fullpath
Open myFile For Input As #1
i=1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
posLat = InStr(text, "ERROR")
Range("A"& i ).Value = Mid(text, posLat + 10, 5)
Loop
Close #1
End Sub
PS:我没有测试Mid(text,posLat + 10,5),所以我不知道到底发生了什么。