Solved

Import PRN file into excel using VBA

Posted on 2010-11-28
9
896 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now