查看txt文件中的具体信息并将其粘贴到Excel中

问题描述 投票:0回答:1

我正在 Excel 中工作,要查找 txt 文件中的某些信息,然后将其粘贴到工作表中。

我想从包含不同定义字符串的不同行复制信息。

示例:
txt 文件就像一本日志。此日志具有类似 |UPDATED|、|CREATED| 的入口和|更新|。记录此信息后,将跟随可变数量的行,直到“主题”用符号“->”关闭。
我希望脚本查找字符串 |UPDATED|,复制整行并粘贴到 Excel 中。然后继续滚动 txt 文件行,直到找到“->”,复制该行并将其放在相应的“标题”下方(已更新、已创建、已更新)。

如果有一种方法也可以记录它找到变量入口的行,那么我可以这样做,例如 const= const - 2,在 txt 文件中查找两行并复制该内容!

Sub PS_automation()

Dim ReadData As String
Dim x As Integer, y As Integer, z As Integer, p As Integer
x = 1
y = 1
z = 1
p = 1

Open "C:\Users\racastr1\Desktop\PS_AUTOMATE.txt" For Input As #1 
    
Do Until EOF(1)
    
    Line Input #1, ReadData 
    
    If InStr(Left(ReadData, 9), "|CREATED|") Then
        Cells(x, 1) = ReadData
        x = x + 1
    Else
        If InStr(Left(ReadData, 8), "|CLOSED|") Then
            Cells(y, 2) = ReadData
            y = y + 1
        Else
            If InStr(Left(ReadData, 9), "|UPDATED|") Then
                Cells(z, 3) = ReadData
                z = z + 1
            End If
        End If
    End If
Loop
Close #1
End Sub

这已经复制了提到的入口,但是如果我有相同的条件来查找“->”,它不会按顺序将其粘贴到工作表中(因此它不会进入关联的“标题”)。

文件示例:
File Example

excel vba
1个回答
1
投票

你可以尝试这样的事情(改变了我最初答案的方法)

Sub PS_automation()
    
    Const TXT_PATH As String = "C:\Users\racastr1\Desktop\PS_AUTOMATE.txt"
    
    Dim data As String, arr, arrCols, i As Long, s As String, col As Long
    Dim ws As Worksheet, n As Long, v, sPrev As String
    
    data = GetContent(TXT_PATH)        'read the whole file
    data = Replace(data, vbcrlf, vbLf) 'normalize line separators
    arr = Split(data, vbLf)            'split to array
    
    'array of label+colNumber pairs
    arrCols = Array("|CREATED|", 1, "|CLOSED|", 2, "|UPDATED|", 3)
    Set ws = ActiveSheet 'or some specific sheet
    
    For i = 0 To UBound(arr) 'loop over the lines from the file
        s = arr(i)   
        'Loop over `arrCols` and check for pre-defined label....
        For n = LBound(arrCols) To UBound(arrCols) Step 2
            v = arrCols(n)            'label
            If Left(s, Len(v)) = v Then  'match?
                col = arrCols(n + 1)  'corresponding column number
                If i > 0 Then 'go back up and check for date?
                    sPrev = arr(i - 1) 'the line above the match
                    If sPrev Like "####-##-## *" Then 'starts with date?
                        'write line with date to sheet if found
                        ws.Cells(Rows.Count, col).End(xlUp).Offset(1).Value = sPrev
                    End If
                End If
                ws.Cells(Rows.Count, col).End(xlUp).Offset(1).Value = s
                GoTo skip 'done with this line
            End If
        Next n
        
        If Left(s, 2) = "->" Then
            If col > 0 Then
                ws.Cells(Rows.Count, col).End(xlUp).Offset(1).Value = s
            End If
        End If
skip:
    Next i
End Sub


Function GetContent(f As String) As String
    GetContent = CreateObject("scripting.filesystemobject"). _
                  OpenTextFile(f, 1).ReadAll()
End Function
© www.soinside.com 2019 - 2024. All rights reserved.