Results 1 to 21 of 21

Copying data from Notepad to excel using VBA-NEED HELP

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-10-2016
    Location
    Pakistan
    MS-Off Ver
    365
    Posts
    242

    Post Copying data from Notepad to excel using VBA-NEED HELP

    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
    Attached Files Attached Files
    Last edited by Zahid0111; 10-26-2023 at 06:55 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Not copying Excel to Notepad as CSV format
    By Ochimus in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-08-2021, 08:29 PM
  2. [SOLVED] Copying and Pasting Data from Excel to Notepad
    By JSH2017 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-26-2018, 01:08 PM
  3. Opening Notepad a saved notepad file and copying the data on it
    By swade730 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2014, 06:18 PM
  4. Copying more than one range of data from excel into more than one notepad file
    By brianQ in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2013, 01:25 PM
  5. Copying Time from Notepad to Excel
    By CindyLisa in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 07-23-2011, 09:07 AM
  6. Copying the data from two notepad files to an Excel workbook
    By raghunaik in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-10-2009, 12:17 AM
  7. copying data from excel into notepad
    By walid66 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-19-2007, 03:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1