'将如下宏代码粘贴到excel的VBA编辑器中,按F5运行即可,注意修改你的文件所在的目录。
Sub ReadData()
Dim Path As String, MyValue As String, fn As Long
Path = "C:\tmp" '假定你的1dr文件处在C:\tmp 文件夹中,可以自行修改
fn = FreeFile
RowI = 1
fname = Dir(Path & "\*.1dr")
'如果是txt文件,则为 fname = Dir(Path & "\*.txt")
If fname <> "" Then
Do
Open Path & "\" & fname For Input As #fn
Row = 0
Do Until EOF(fn)
Line Input #fn, Data
Data = Trim(Data)
If Data <> "" Then Row = Row + 1
If Row = 6 Then Exit Do '这里6表示表示提取第几行的数据
Loop
Close #fn
If Data <> "" And Row = 6 Then
Data = Replace(Data, " ", vbTab)
Data = Replace(Data, vbTab & vbTab, vbTab)
Data = Split(Data, vbTab)
n = UBound(Data)
If n >= 10 And Trim(Data(9)) <> "" Then '提取第6行第10列数据填充到单元格的第1列中
On Error Resume Next
Cells(RowI, 1).Value = Data(9)
If Err.Number Then
Cells(RowI, 1) = Data(9)
End If
End If
If n >= 1 And Trim(Data(0)) <> "" Then '提取第6行第1列数据填充到单元格的第2列中
On Error Resume Next
Cells(RowI, 2).Value = Data(0)
If Err.Number Then
Cells(RowI, 2) = Data(0)
End If
End If
If n >= 13 And Trim(Data(12)) <> "" Then '提取第6行第13列数据填充到单元格的第3列中
On Error Resume Next
Cells(RowI, 3).Value = Data(12)
If Err.Number Then
Cells(RowI, 3) = Data(12)
End If
End If
End If
RowI = RowI + 1
fname = Dir()
Loop While fname <> ""
ActiveWorkbook.Save
End If
MsgBox "处理完毕!", vbInformation + vbOKOnly, "消息"
End Sub
追问好棒!! 我和小伙伴们都惊呆了!谢谢大神啦!
只是运行后,发现第一个txt的提取好像往前进了一列,从第10列和13列变为了9和12列(第1列是对的)。
一阵测试后,发现这个是因为第一个txt文件的某一列数据(60值此列)前面有3个空格,而第二个TXT及后面文件都没有存在三个空格的情况(60值变为了120等三位数),所以是因为三个空格的问题,请问这个可以修改吗?非常感谢!



谢谢!
追答for ii=0 to 10
Data = Replace(Data, " ", vbTab)
Data = Replace(Data, vbTab & vbTab, vbTab)
next
来自:求助得到的回答