Link to home
Start Free TrialLog in
Avatar of sq30
sq30

asked on

Import PRN file into excel using VBA

Hello,

How can I use excel VBA to prompt me to select a TXT or PRN file to import in to an excel spreadsheet. The source data format is in testdata.txt and I'd like it imported into a table as laid out in excel-test-dat.xls with the date colums formated as "date" and any totals headers or footers imported from the source data deleted.

Is this possible?

Many thanks
sq
TESTDATA.txt
Excel-test-data.xls
Avatar of SiddharthRout
SiddharthRout
Flag of India image

You may use GetOpenFilename Method for that for example

Sub Sample()
    Dim ret
    
    ret = Application.GetOpenFilename("Text Files (*.txt),*.txt,Prn Files (*.prn),*.prn")
End Sub

Open in new window

The code that I gave you above is "prompting" for opening the file.

However, if you want to get the text from the text file then there can be a different approach...

That approach would be reading a text file in one go in a variable for example...

Sub Sample()
    Dim MyData As String, strData() As String, Search As String
    Dim I As Long
    
    Open "MyTextFile.Txt" For Binary As #1
    MyData = Space$(LOF(1))
    Get #1, , MyData
    Close #1
    
    strData() = Split(MyData, vbCrLf)
    For I = 0 To UBound(strData())
        '~~> Your code here to read from text file
    Next
End Sub

Open in new window

Avatar of sq30
sq30

ASKER

Hi SiddharthRout,

Thanks for your responce but I need help with the whole thing. Neither of your responces are helping me.

Sorry
Sq
sq30: My both post can help you out with your query if you actually understood them :)

The 2nd post is what you actually want. The text file is stored in the array strData()

All you now need to do is loop through the contents of the array and extract the relevant information and put them in the Excel cells. I could have given you the entire code but I wanted you to try it first...

Give it a try and if you get stuck simply post the code that you tried and I will definitely help you :)

Sid
Avatar of Jacques Geday
Hi sq30,
Can you pls advise if the sample you posted is 'always' the same format that you will need to import into Excel or you simply need a code that will import into Excel 'ANY' format that you will highlight !??
Tks/gowflow
Avatar of sq30

ASKER

Hi gowflow,

The format of the rows should stay the same but being able to select any format highlighed sounds interesting.
I've managed to piece together some code that I found on EE that allows me to treat the import as fixed width.  The problems I have now is that I'd like to delete the rows in red automatically  (see test1.xls) which will not always be in the same position plus when the file is saved the link to the external data is saved which I'd like to stop. I'm note sure if that's to do with it creating a named range?

Any help or better solution to what I've cobbled together would be greatly appreciated.
Sq30
Test1.xls
ASKER CERTIFIED SOLUTION
Avatar of Jacques Geday
Jacques Geday
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sq30

ASKER

Hi gowflow,
Excellent, thank you so much. I can understand the criteria in your testline function, so i should be able to tweak it at a later date if the layout of the report changes.
Many thanks
sq30
Your welcome anytime
rgds/gowflow