Solved

Import PRN file into excel using VBA

Posted on 2010-11-28
9
984 Views
Last Modified: 2012-05-10
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
0
Comment
Question by:sq30
  • 3
  • 3
  • 3
9 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34226758
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

0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34226815
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

0
 

Author Comment

by:sq30
ID: 34226958
Hi SiddharthRout,

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

Sorry
Sq
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:SiddharthRout
ID: 34226965
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
0
 
LVL 29

Expert Comment

by:gowflow
ID: 34234071
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
0
 

Author Comment

by:sq30
ID: 34234977
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
0
 
LVL 29

Accepted Solution

by:
gowflow earned 500 total points
ID: 34237571
Hi Sq30,

I hope that this is what ur looking for pls let me know.
Rgds/gowflow
Test1.xls
0
 

Author Closing Comment

by:sq30
ID: 34241288
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
0
 
LVL 29

Expert Comment

by:gowflow
ID: 34241304
Your welcome anytime
rgds/gowflow
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question