Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2004
    Posts
    30

    Unanswered: Building Parsers

    I will be working on building parsers soon that takes a file out of notepad (.txt) and imports it into excel.

    My form will have a browse button, browse to file on your hard drive (a .txt) and then click open. Then on the form you will click the parse button and it will parse it out.

    Whats the easiest way to do this? Can I manually parse it and then look at the code w/ alt+F11?

  2. #2
    Join Date
    Jun 2004
    Location
    Terrapin Nation
    Posts
    205
    "Whats the easiest way to do this? Can I manually parse it and then look at the code w/ alt+F11?"









    If you mean record a macro and then look at the code to modify it the answer would be no. The code wil show you how to browse for the file, thats about it.

    You should consider using access. All you would have to do is import the file to a table.

  3. #3
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    Quote Originally Posted by joshua310
    My form will have a browse button, browse to file on your hard drive (a .txt) and then click open. Then on the form you will click the parse button and it will parse it out.
    The easiest way to do this bit is to use the Application.GetOpenFile code
    Once you get the text file you can use workbook.opentext
    Search for these in the help file

    Quote Originally Posted by joshua310
    Whats the easiest way to do this? Can I manually parse it and then look at the code w/ alt+F11?
    Depends if there is a consequitve delimiter or fixed width not if there is you can use the TextToColumns property on your selected range but without knowing more i won't be able to be of too much more help

    David

  4. #4
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1

    Reading and pasing a file

    Hi

    Following on from DavidCotts couts suggestion I have listed below a modified extract I have written to do just what you have indicated.

    This is based on a form having two command buttons and a text box (you will also need to show the form).

    Code:
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim File As String
         
        ChDir ThisWorkbook.Path
        File = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "Select Text File")
        
        If File = "False" Then Exit Sub
        
        If Dir(File) = "" Then
            MsgBox File & " Not found !"
        Else
            TextBox1 = File
        End If
    
        
    End Sub
    
    Private Sub CommandButton2_Click()
        Dim Text As String
        
        If Dir(TextBox1) = "" Then
            MsgBox File & "File Path & Name must be specied !"
        Else
                
            Open TextBox1 For Input As #1 ' OPEN FILE FOR READING
                    
            Line Input #1, Text ' READ FIRST LINE
            
            If Text <> "??" Then
                MsgBox "File not compatable etc"
            Else
                Do While Not EOF(FileNo)
                    'PARSE EACH FILE LINE HERE AND WRITE TO ACTIVE SHEET !!
                Loop
            End If
        
            Close #1   'CLOSE FILE
        End If
    End Sub

    As indicated, information is available in the VBA Help files for both GetOpenFileName and the Open Method for reading a file on disk one line at a time.

    Of course we do not know what procedure you require to parse each line.

    This code also relies on the first line odf the file having a unique id to verifiy it is the correct type of file to be parsed!

    Just one way of doing it!

    HTH

    MTB

  5. #5
    Join Date
    Jan 2004
    Location
    Aberdeen, Scotland
    Posts
    1,067
    Quote Originally Posted by MikeTheBike
    Following on from DavidCotts couts suggestion I have listed below a modified extract I have written to do just what you have indicated.
    2 tries and still you couldn't get it right, Ive been so short of time im starting to give more and more criptic answers

    Joshuha post what some of yur text look like and how you want it parsed then we can look at ways of parsing it for you

    David

Posting Permissions

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