使用VBA将文本文件中的精确信息转换为excel列
[在此处输入链接说明][1]我正在尝试搜索此文本文件中的特定单词,以便在excel列中输出其行内容。文本文件包含多个部分。我可以输出文本文件的第一部分,但由于某些原因,我无法定义循环以便检索文件的每个部分 到目前为止,我的代码是:使用VBA将文本文件中的精确信息转换为excel列,vba,excel,Vba,Excel,[在此处输入链接说明][1]我正在尝试搜索此文本文件中的特定单词,以便在excel列中输出其行内容。文本文件包含多个部分。我可以输出文本文件的第一部分,但由于某些原因,我无法定义循环以便检索文件的每个部分 到目前为止,我的代码是: Sub test() Dim myFile As String, text As String, textline As String, DDC As Integer, DDR As Integer, DDP As Integer, ADC As Integer, i
Sub test()
Dim myFile As String, text As String, textline As String, DDC As Integer, DDR As Integer, DDP As Integer, ADC As Integer, i As Integer, SE As Integer, SP As Integer, SG As Integer, j As Integer, v As Integer
myFile = "C:\Users\Seb\Desktop\text2.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
i = 1
DDC = InStr(text, "Date de calcul")
DDR = InStr(text, "Date de retraite")
ADC = InStr(text, "Âge à la date du calcul")
SE = InStr(text, "Service d'emploi")
SP = InStr(text, "Service de participation")
SG = InStr(text, "Salaire gagné")
Cells(i + 1, 1).Value = Mid(text, DDC, 14)
Cells(i + 1, 2).Value = Mid(text, DDC + 36, 10)
Cells(i + 2, 1).Value = Mid(text, DDR, 16)
Cells(i + 2, 2).Value = Mid(text, DDR + 36, 10)
Cells(i + 3, 1).Value = Mid(text, ADC, 23)
Cells(i + 3, 2).Value = Mid(text, ADC + 36, 6)
Cells(i + 4, 1).Value = Mid(text, SE, 16)
Cells(i + 4, 2).Value = Mid(text, SE + 36, 6)
Cells(i + 5, 1).Value = Mid(text, SP, 24)
Cells(i + 5, 2).Value = Mid(text, SP + 36, 6)
For v = 0 To 10
j = v * 228
Cells(v + 7, 1).Value = Mid(text, SG + j, 24) + Mid(text, SG + 64 + j, 10) + "/ " + Mid(text, SG + 77 + j, 10)
Cells(v + 7, 2).Value = Mid(text, SG + 103 + j, 10)
Next v
End Sub
此处提供了我的文本文件示例:
正如我前面提到的,我只能在excel中输出第1节。为了检索文本文件的每个部分,我的代码应该是什么 覆盖每个部分后,只需从
文本
字符串中删除覆盖部分,以便在下一次迭代中,例如InStr(text,“Date 1”)
将找到下一部分的Date 1
行
Do While True
DDC = InStr(text, "Date 1")
If DDC = 0 Then
' no more sections - exit loop
Exit Do
End If
DDR = InStr(text, "Date 2")
ADC = InStr(text, "Age")
' ......
Next v
' remove the section that was just handled
text = Mid(text, SG + 30)
Loop
如果将TXT文件作为数据引入► 外部数据► 从文本中,可以将句点设置为其他分隔符(将连续分隔符视为True)
子导入_Text()
Dim c为长,myFile为字符串
myFile=“C:\Users\Seb\Desktop\text.txt”
对于工作表(“Sheets 9”)“回到上一个问题如何?我不明白代码的这部分如何删除一个部分”“删除刚刚处理过的部分text=Mid(text,SG+30)@mckaymental:只需在逐步执行代码时尝试一下即可。”SG
指向“工资”行的开头,SG+30
即将结束Mid()
不带第三个参数将获取字符串的所有内容,从第二个参数开始一直到最后。它可以工作,但我获得的第一个数据将被Excel中的第二个集擦除。我的档案里也有很多这样的章节,我怎样才能把它们都弄到呢?我把我真实档案的一部分放在这里
Sub Import_Text()
Dim c As Long, myFile As String
myFile = "C:\Users\Seb\Desktop\text.txt"
With Worksheets("Sheet9") '<~~set this worksheet reference properly!
With .QueryTables.Add(Connection:="TEXT;" & myFile, _
Destination:=Range("$A$1"))
.Name = "TXT"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "."
.TextFileColumnDataTypes = Array(1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
'these will cleanup (trim) the results
For c = 1 To 2
With .Columns(c)
.TextToColumns Destination:=.Cells(1), DataType:=xlFixedWidth, _
FieldInfo:=Array(0, 1), TrailingMinusNumbers:=True
End With
Next c
End With
End Sub