我想构建一个宏,该宏可以从文本文件读取数据模式并在电子表格上进行更新。
文本文件
Client: shubham chaturvedi
File Name: CONDARMIT20181030105226.xml
EDI Reference Number:
Temp Incident ID: ARMIT1810301012
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: ARMIT1810301012 already exists.
Client: anupam chaturvedi
File Name: CONDARCHR20181030125104.xml
EDI Reference Number:
Temp Incident ID: ARCHR1810301007
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: ARCHR1810301007 already exists.
Client: anupam chaturvedi
File Name: CONDARCHR20181108200819.xml
EDI Reference Number:
Temp Incident ID: ARCHR1811081013
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: ARCHR1811081013 already exists.
Client: Gunjan sharma
File Name: CONDGunjan sharma20181030152228.xml
EDI Reference Number:
Temp Incident ID: Gunjan sharma1810291003
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: Gunjan sharma1810291003 already exists.
Client: Vinayak Chaturvedi
File Name: Retry_Retry_Retry_CONDCHART20181125125646.xml
EDI Reference Number:
Temp Incident ID: CHART1811251556
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: CHART1811251556 already exists.
Client: Vinayak Chaturvedi
File Name: CONDCHART20181108125939.xml
EDI Reference Number:
Temp Incident ID: CHART1811081252
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: CHART1811081252 already exists.
Client: Vinayak Chaturvedi
File Name: CONDCHART20181108175802.xml
EDI Reference Number:
Temp Incident ID: CHART1811081263
Element Name: xyskjd/kjdhsjhk/jhuyiijljf
Attribute:
Field Number: 1001
Validation Error: 901
Error: CHART1811081263 already exists.
我只想取Temp Incident ID:
,Error:
和Element Name:
之后的值
在电子表格上进行更新。
我只能使用此代码获得一次结果。请帮助我提供可以读取整个文件数据的代码。
Option Explicit
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posElement As Integer, posError As Integer
myFile = "C:\test\NFR.txt"
'myFile = Application.GetOpenFilename()
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
posElement = InStr(text, "Element")
posError = InStr(text, "Error")
Range("A1").Value = Mid(text, posElement + 14, 36)
Range("B1").Value = Mid(text, posError + 16, 32)
End Sub
答案 0 :(得分:1)
如果我理解您的问题,可以尝试以下代码:
编辑代码:以回答您的评论
Private Sub CommandButton1_Click()
Dim myFile As String, text As String, textline As String, posElement As Integer, posError As Integer, posTime As Integer
Dim i As Integer
Dim ok As Boolean
ok = False ' there are two string with "Error" word
i = 1 ' use the number for write into new row the data...
myFile = "C:\test\NFR.txt"
'myFile = Application.GetOpenFilename()
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
'text = text & textline
text = textline
'MsgBox text
posTime = InStr(text, "Temp Incident ID")
posElement = InStr(text, "Element")
posError = InStr(text, "Error")
'control the Temp incident ID word
If posTime <> 0 Then
Cells(i, 1) = Mid(text, posTime + 18, 33)
'i = i + 1
End If
'control the Element word
If posElement <> 0 Then
Cells(i, 2) = Mid(text, posElement + 14, 36)
' i = i + 1
End If
'this code control the Error word
If ok And posError <> 0 Then
Cells(i, 3) = Mid(text, posError + 7, 32)
i = i + 1
End If
If posError <> 0 Then
ok = Not (ok)
End If
Loop
Close #1
'auto Fit column
Columns("A:C").Select
Selection.EntireColumn.AutoFit
End Sub
在文件txt中有两行带有错误词,因此我使用了布尔变量“ ok”。我已经使用“ i”变量将被贬低的文本写成行
希望这会有所帮助