从文本文件复制数据(从PDF文件中提取的特定模式)

Copying data from text file(which is in a specific pattern extracted from PDF file)

提问人:ZahidHussain 提问时间:10/29/2023 最后编辑:freeflowZahidHussain 更新时间:10/29/2023 访问量:51

问:

需要支持将数据从文本文件复制到 excel sheet1。在文本文件中,数据处于特定模式,并希望从中提取一些数据到excel。所需的结果是手动添加到附件照片中。还附上文本文件照片以供参考。由于它是一个大数据,无法手动复制,因此需要一个VBA解决方案。

Required data my data text file

文件链接:数据文件

我已经尝试了这段代码,但它带来了完整的数据。

Sub CopyDataFromTextFile()
    Dim FilePath As String
    Dim DataPattern As String
    Dim DataArray() As String
    Dim i As Long
    Dim LastRow As Long
    Dim ws As Worksheet
    Dim CopyLine As Boolean
    
    ' Set the file path of the text file
    FilePath = "C:\Users\engr_\Desktop\Data4.txt"
    
    ' Set the data pattern to look for
    DataPattern = "ETHERCAT NETWORK|CAVO ETHERNET CAT6A 10 GBIT RJ45/RJ45|2549850282|1|TO BE ADDED\TO BE REMOVED"
    
    ' Split the data pattern into an array
    DataArray = Split(DataPattern, "|")
    
    ' Set the worksheet to paste the data into
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change the sheet name as needed
    
    ' Open the text file for reading
    Open FilePath For Input As #1
    
    ' Initialize variables
    LastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row + 1
    CopyLine = False
    
    ' Loop through the text file
    Do While Not EOF(1)
        Dim Line As String
        Line Input #1, Line
        ' Check if the line matches the data pattern
        For i = LBound(DataArray) To UBound(DataArray)
            If InStr(1, Line, DataArray(i), vbTextCompare) > 0 Then
                CopyLine = True
                Exit For
            End If
        Next i
        ' If CopyLine is True, copy the line to the worksheet
        If CopyLine Then
            ws.Cells(LastRow, 2).Value = Line
            LastRow = LastRow + 1
        End If
    Loop
    
    ' Close the text file
    Close #1
End Sub

VBA Excel-2010 导出到 Excel

评论

0赞 CDP1802 10/29/2023
添加另一个内部循环,例如在CopyLine = FalseLastRow = LastRow + 1
0赞 ZahidHussain 10/29/2023
同样,它复制完整的数据并将其粘贴到 B 列中。
0赞 CDP1802 10/29/2023
尝试只用DataPattern = "ETHERCAT NETWORK"
0赞 taller 10/29/2023
重置为@CDP1802注释应该可以工作。请注意,在.它不仅在列中匹配,而且在其他列中匹配(例如。 在列中)。提取相关线后,您有什么计划?如何拆分?CopyLineFalse|1|DataPattern1QTY0150401LOCATION
0赞 ZahidHussain 10/29/2023
@CDP1802更改数据模式后仅复制ETHERCAT NETWORK,您可以检查附件中的文件吗,它显示了我想要的内容

答:

0赞 taller 10/29/2023 #1

文本文件中的数据结构不合理,因此很难确定如何拆分每一行。

注意:请查看输出中的部分,因为它可能需要微调。TECHNICAL DESCRIPTION

Option Explicit

Sub Deomo()
    Dim FilePath As String
    Dim csvWK As Workbook, csvSht As Worksheet
    Dim arrData, arrRes(), sKey
    Dim i As Long, j As Long
    Const KEY1 = "TO BE ADDE"
    Const KEY2 = "TO BE REMO"
    Application.ScreenUpdating = False
    FilePath = "d:\TEMP\Data4.txt" ' Modify as needed
    ' Split by widht
    Workbooks.OpenText Filename:=FilePath, Origin:=xlWindows, _
        StartRow:=1, DataType:=xlFixedWidth, _
        FieldInfo:=Array(Array(0, 1), Array(10, 1), Array(56, 1), _
        Array(100, 1), Array(110, 1), Array(128, 1))
    Set csvWK = ActiveWorkbook
    ' Load data
    With csvWK.ActiveSheet
        arrData = .UsedRange.Value
    End With
    csvWK.Close False
    ReDim arrRes(1 To 5, 1 To UBound(arrData))
    j = 1
    ' Populate header
    For Each sKey In Split("DESCRIPTION|TECHNICAL DESCRIPTION|PARTS CODE|QTY|TO BE ADDED/REMOVED", "|")
        arrRes(j, 1) = sKey
        j = j + 1
    Next sKey
    j = 1
    For i = LBound(arrData) To UBound(arrData)
        sKey = Trim(arrData(i, 4))
        ' Matching keyword
        If sKey = KEY1 Or sKey = KEY2 Then
            j = j + 1
            ' TO BE ADDED/REMOVED
            arrRes(5, j) = sKey & IIf(sKey = KEY1, "D", "VED")
            ' QTY
            arrRes(4, j) = arrData(i - 1, 6)
            ' PARTS CODE
            arrRes(3, j) = "'" & Trim(arrData(i - 1, 5))
            ' TECHNICAL DESCRIPTION
            arrRes(2, j) = arrData(i - 1, 3) & arrData(i - 1, 4)
            ' DESCRIPTION
            arrRes(1, j) = arrData(i, 2)
        End If
    Next i
    ReDim Preserve arrRes(1 To 5, 1 To j)
    ' Write data to work sheet
    With ActiveSheet
        .Cells.Clear
        .Range("A1").Resize(j, 5).Value = Application.Transpose(arrRes)
        .Columns("A:E").AutoFit
    End With
    Application.ScreenUpdating = True
End Sub

enter image description here

Microsoft 文档:

Workbooks.OpenText 方法 (Excel)

评论

0赞 ZahidHussain 10/29/2023
谢谢你@taller。这工作正常。:)我怎样才能为你添加声誉,因为我尝试通过按箭头符号添加,但它根本没有添加:(