Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Import PRN file into excel using VBA

Posted on 2010-11-28
9
Medium Priority
?
1,156 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
Independent Software Vendors: 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 31

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 31

Accepted Solution

by:
gowflow earned 2000 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 31

Expert Comment

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

886 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