我试图从一个txt文件中提取数据,并将它们放入一个结构化的Excel表中。txt文件看起来像这样。
Date 28.07.2022 Time: 16:52
New File
Date 28.07.2022 Time: 16:52
A: 83.24
B 2220900102 C 30.23
D=6 E=3 F=130
G -14.9
H 0.006
I -0.004
J 83.287 K 83.268
...
虽然我能够提取第一组数据,但我无法在表中显示以下任何一组数据。我能找到的最接近我的问题的是this,但除非我错过了,否则他们告诉他要做的唯一事情就是在代码中实现一个循环。我试着这样做,到目前为止我的代码看起来是这样的。
Sub Button()
Dim myFile As String, text As String, textline As String
Dim posA As Integer, posB As Integer, ...
Dim i As Long
myFile = "Path\myFile.TXT"
Open myFile For Input As #1
i = 1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
posDate = InStr(text, "Date")
If posDate = 1 Then
i = i + 1
End If
posTime = InStr(text, "Time")
posA = InStr(text, "A")
...
Cells(i, 1).Value = Mid(text, posDate + 5, 10)
Cells(i, 2).Value = Mid(text, posTime + 6, 5)
Cells(i, 3).Value = Mid(text, posA + 27, 5)
...
Loop
Close #1
End Sub
我不知道如何改变它,因为我有很少的经验与vba。
1条答案
按热度按时间cngwdvgl1#
Try using a Regular Expression.