+ Reply to Thread
Results 1 to 6 of 6

Copy data from notepad into excel

Hybrid View

  1. #1
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    22

    Copy data from notepad into excel

    hi

    I have a folder with a lot of notepad files. I need to extract certain info from each file for example as per attached notepad file opened in excel
    see sheet 2
    company name is the heading and below it I want to copy the company name etc.

    Is it possible to this with a macro ?
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Copy data from notepad into excel


    Hi,

    if text files, it's possible, just read VBA inner help of Open, Input # and Line Input # statements …

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy data from notepad into excel

    Hello platesigns,

    Is the Notepad file on sheet 8f30f76f-e047-489a-9e6f-43f0ade?

    If so then your text file is actually the page source for an HTML file. Is this format the same for all files in the folder?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Registered User
    Join Date
    05-15-2014
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: Copy data from notepad into excel

    hi

    yes the notepad file I imported into sheet 8f30f76f-e047-489a-9e6f-43f0ade
    but the problem is some files have less lines then others and some have more so it complicates it for me if I search by line no.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy data from notepad into excel

    Hello platesigns,

    The attached workbook has the macro code shown below installed. There is a button on "Sheet1" to run the macro.

    The macro assumes the text files are in the same folder as the workbook. If the files are not in this folder, change the macro variable Folderpath (in blue font) to where your files are saved.

    Module1 Macro Code
    Sub ParseHTML(ByVal FileToOpen As String, ByRef Dest As Range)
    
        Dim Data(4)     As String
        Dim Filename    As String
        Dim HTMLdoc     As Object
        Dim n           As Long
        Dim oDiv        As Object
        Dim PageSrc     As String
        Dim URL         As String
            
            URL = "file:///" & FileToOpen
            URL = Replace(URL, "\", "/")
            
                With CreateObject("MSXML2.XMLHTTP")
                    .Open "GET", URL, False
                    .Send
                    PageSrc = .ResponseText
                End With
                
                Set HTMLdoc = CreateObject("HTMLfile")
                HTMLdoc.Open
                HTMLdoc.Write PageSrc
                
                For Each oDiv In HTMLdoc.getElementsByTagName("div")
                    If oDiv.classname = "row" Then
                        For n = 0 To oDiv.ChildNodes.Length - 1
                            If oDiv.ChildNodes(n).NodeType = 1 Then
                                Select Case oDiv.ChildNodes(n).innerHTML
                                    Case Is = "Contractor Name:"
                                        Data(0) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Contact Person:"
                                        Data(1) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Telephone:"
                                        Data(2) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "E-Mail Address:"
                                        Data(3) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Fax Number:"
                                        Data(4) = oDiv.ChildNodes(n + 1).innerHTML
                                End Select
                            End If
                        Next n
                    End If
                Next oDiv
                
            Dest.Value = Data()
            
    End Sub
    
    Sub CopyFileData()
    
        Dim File        As Variant
        Dim Folderpath  As Variant
        Dim oFiles      As Object
        Dim oFolder     As Object
        Dim oShell      As Object
        Dim Rng         As Range
        Dim Wks         As Worksheet
        
            Folderpath = ThisWorkbook.Path
            
            Set Wks = Worksheets("Sheet1")
            
            Set Rng = Wks.Range("A2:E2")
            
                Wks.UsedRange.Offset(1, 0).ClearContents
    
                Set oShell = CreateObject("Shell.Application")
                Set oFolder = oShell.Namespace(Folderpath)
                
                If oFolder Is Nothing Then
                    MsgBox "The Folder " & Folderpath & " Was Not found." & vbCrLf _
                            & "Please Check that the Path is Correct.", vbOKO + vbExclamation
                    Exit Sub
                End If
                
                    Set oFiles = oFolder.Items
                
                    oFiles.Filter 64, "*.txt"
        
                    For Each File In oFiles
                        Call ParseHTML(File.Path, Rng)
                        Set Rng = Rng.Offset(1, 0)
                    Next File
                
    End Sub
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy data from notepad into excel

    Hello platesigns,

    The attached workbook will open all text files in the same folder that the workbook is saved in. This will occur when the workbook opens or you manually click the button on "Sheet1".

    You can change the folder location in the macro CopyFileData in Module1. If your worksheet name is not "Sheet1" then you will need to change this match your sheet's name.

    How this works is each text file is read and converted into an HTML file. The elements of the HTML file are scanned and the data is collected. The collected data is the output to the next empty row of the worksheet.

    Here are the macros...

    ThisWorkbook Module
    Private Sub Workbook_Open()
        Call Module1.CopyFileData
    End Sub
    Moudle1 Macros
    Sub ParseHTML(ByVal FileToOpen As String, ByRef Dest As Range)
    
        Dim Data(4)     As String
        Dim Filename    As String
        Dim HTMLdoc     As Object
        Dim n           As Long
        Dim oDiv        As Object
        Dim PageSrc     As String
        Dim URL         As String
            
            On Error GoTo ErrHandler
            
            URL = "file:///" & FileToOpen
            URL = Replace(URL, "\", "/")
            
              ' Open the text file and copy the HTML text in a string variable.
                With CreateObject("MSXML2.XMLHTTP")
                    .Open "GET", URL, False
                    .Send
                    PageSrc = .ResponseText
                End With
                
              ' Convert the HTML page source text into an HTML file.
                Set HTMLdoc = CreateObject("HTMLfile")
                HTMLdoc.Open
                HTMLdoc.Write PageSrc
                
              ' Examine the HTML elements in the HTML file.
                For Each oDiv In HTMLdoc.getElementsByTagName("div")
                    If oDiv.classname = "row" Then
                        For n = 0 To oDiv.ChildNodes.Length - 1
                            If oDiv.ChildNodes(n).NodeType = 1 Then
                                Select Case oDiv.ChildNodes(n).innerHTML
                                    Case Is = "Contractor Name:"
                                        Data(0) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Contact Person:"
                                        Data(1) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Telephone:"
                                        Data(2) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "E-Mail Address:"
                                        Data(3) = oDiv.ChildNodes(n + 1).innerHTML
                                    Case Is = "Fax Number:"
                                        Data(4) = oDiv.ChildNodes(n + 1).innerHTML
                                End Select
                            End If
                        Next n
                    End If
                Next oDiv
                
          ' Copy the parsed information to the worksheet.
            Dest.Value = Data()
            
    ErrHandler:
            If Err <> 0 Then
                MsgBox "Run-time error '" & Err.Number & "':" & vbCrLf & vbCrLf & Err.Description
            End If
            
    End Sub
    
    Sub CopyFileData()
    
        Dim File        As Variant
        Dim Folderpath  As Variant
        Dim oFiles      As Object
        Dim oFolder     As Object
        Dim oShell      As Object
        Dim Rng         As Range
        Dim Wks         As Worksheet
        
          ' Change this to the folder with your files.
            Folderpath = ThisWorkbook.Path
            
          ' Change the worksheet name if it is not Sheet1.
            Set Wks = Worksheets("Sheet1")
            
            Set Rng = Wks.Range("A2:E2")
            
              ' Clear any previous files' data.
                Wks.UsedRange.Offset(1, 0).ClearContents
    
                Set oShell = CreateObject("Shell.Application")
                Set oFolder = oShell.Namespace(Folderpath)
                
                If oFolder Is Nothing Then
                    MsgBox "The Folder " & Folderpath & " Was Not found." & vbCrLf _
                            & "Please Check that the Path is Correct.", vbOKO + vbExclamation
                    Exit Sub
                End If
                
                  ' Return all the folder and files in the selected folder.
                    Set oFiles = oFolder.Items
                
                  ' Leave only a list of Text Files in this folder.
                    oFiles.Filter 64, "*.txt"
                    
                    If oFiles.Count = 0 Then
                        MsgBox "No Text Files Were found in Folder:" & vbCrLf & Folderpath, vbOKOnly + vbExclamation
                        Exit Sub
                    End If
        
                  ' Use CTRL+Break to interrupt the loop.
                    For Each File In oFiles
                        DoEvents
                        Call ParseHTML(File.Path, Rng)
                        Set Rng = Rng.Offset(1, 0)
                    Next File
                
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. vbscript to copy data from excel to notepad
    By samiulla.warimani in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-20-2014, 10:27 PM
  2. Macro to copy data from excel to notepad
    By emmamaki in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-19-2014, 03:42 AM
  3. [SOLVED] Copy excel data to notepad.
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2012, 08:56 AM
  4. [SOLVED] How do i copy columns of data in notepad into excel?
    By JJ in forum Excel General
    Replies: 1
    Last Post: 02-10-2005, 06:06 PM
  5. [SOLVED] How do i copy columns of data in notepad into microsoft excel?
    By JP in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 02-10-2005, 06:06 PM

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