Excel 用vba从测试文件中读取图形

Excel 用vba从测试文件中读取图形,excel,vba,Excel,Vba,我想建立一个宏,它可以读取文本文件中的数据模式,并在电子表格上进行更新 文本文件 Client: shubham chaturvedi File Name: CONDARMIT20181030105226.xml EDI Reference Number: Temp Incident ID: ARMIT1810301012 Element Name: xyskjd/kjdhsjhk/jhuyiijljf Attribute: Field Number: 1001 Validat

我想建立一个宏,它可以读取文本文件中的数据模式,并在电子表格上进行更新

文本文件

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.
我只想在临时事件ID:、错误和元素名称之后添加值: 要在电子表格上更新

我可以通过使用此代码获得结果,但只能使用一次。请帮助我的代码,可以读取整个文件的数据

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

如果我理解您的问题,您可以尝试以下代码:

执行宏后,您将得到以下结果:编辑图像

编辑代码:回答您的评论

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变量将所需的文本写入行中


希望这能有所帮助

我将属性分离出来,以便有自己的行。如果每个条目都作为一行输入到文本文件中,请随时恢复我的editworks!!非常感谢你为我提供的帮助。如果我可以在单独的列中导入相同的内容,那就太好了。A、 B和CI已经根据您的需要更新了代码…如果您想从第2行开始数据,您可以在变量i中写入值2,以便在i=2中传递i=1。我添加的最后一行代码自动调整列…对我有效。。。非常感谢。