Hello team,
I have almost 600 pages of PDF from which i copied the data to notepad(to make it easy to copy to excel). I want to copy specific data from notepad to excel sheet. I have written VBA code but its not working. Below is the code and attached are the screenshots along with the files for reference(sample data).
It should only copy highlighted data from each line, highlighted for understanding purpose the same goes for all the data.
notepad data.PNG
Sub CopyDataFromNotepad()
Dim FilePath As String
Dim FileContent As String
Dim Lines() As String
Dim Line As String
Dim i As Long
Dim Description As String
Dim TechnicalDescription As String
Dim ToBeAddedOrRemoved As String
Dim Qty As String
Dim RowCounter As Long
' Define the file path
FilePath = "C:\Users\engr_\Desktop\Data.txt" ' Replace with the actual file path
' Read the content of the Notepad file
Open FilePath For Input As #1
FileContent = Input$(LOF(1), 1)
Close #1
' Split the content into lines
Lines = Split(FileContent, vbNewLine)
' Initialize variables
Description = ""
TechnicalDescription = ""
ToBeAddedOrRemoved = ""
Qty = ""
RowCounter = 1
' Loop through the lines
For i = LBound(Lines) To UBound(Lines)
Line = Trim(Lines(i))
' Look for specific keywords and copy data accordingly
If InStr(1, Line, "Location:") > 0 Then
Location = Trim(Lines(i + 1))
TechnicalDescription = Trim(Lines(i + 2))
CONSISTSOF: SPAREPARTS = Trim(Lines(i + 3))
ElseIf InStr(1, Line, "TECHNICAL DESCRIPTION:") > 0 Then
TechnicalDescription = Trim(Lines(i + 1))
ElseIf InStr(1, Line, "CONSISTS OF: SPARE PARTS CODE") > 0 Then
ToBeAddedOrRemoved = Trim(Lines(i + 1))
ElseIf InStr(1, Line, "QTY") > 0 Then
Qty = Trim(Lines(i + 3))
End If
' Check if we have all the required data
If Description <> "" And TechnicalDescription <> "" And ToBeAddedOrRemoved <> "" And Qty <> "" Then
' Paste the data in the Excel sheet
Worksheets("Sheet1").Cells(RowCounter, 1).Value = Description
Worksheets("Sheet1").Cells(RowCounter, 2).Value = TechnicalDescription
Worksheets("Sheet1").Cells(RowCounter, 3).Value = ToBeAddedOrRemoved
Worksheets("Sheet1").Cells(RowCounter, 4).Value = Qty
' Reset variables for the next set of data
Description = ""
TechnicalDescription = ""
ToBeAddedOrRemoved = ""
Qty = ""
' Increment the row counter
RowCounter = RowCounter + 1
End If
Next i
End Sub
Bookmarks