Solved

Import PRN file into excel using VBA

Posted on 2010-11-28
9
1,007 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 30

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 30

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 30

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

752 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