Solved

Excel VBA macro that can import any text file to Excel

Posted on 2013-12-11
6
734 Views
Last Modified: 2013-12-14
Hi

Is it possible to build a macro that can import any text file to Excel

I have attached an example text file

Thanks
Order-Details.txt
0
Comment
Question by:murbro
[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
6 Comments
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39711304
In answer to your question, is it possible, Yes it is.

I think you need to give some more detail about what you want the macro to do with the text file.

Likeiwse, why have a macro when there is already the standard text import wizard that will convert text files into columns dependent on separators or column widths unless you then want to perform some tasks on the data once imported.

Example file looks like a fairly standard Comma Separated Values file so the Import wizard will handle it fairly easily using comma as the separator.

Thanks
Rob H
0
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39711308
Hi,

pls try
Sub Macro1()
'
FileToOpen = Application _
 .GetOpenFilename("Text Files (*.txt), *.txt")
If FileToOpen = False Then Exit Sub
 

 With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;" & FileToOpen, _
        Destination:=Range("$A$1"))
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
    End With

End Sub

Open in new window

Regards
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39711371
Here you are !!!
Rgonzo1971 did it for you in no sweat ! :)

Just a small note this would apply for the specific file type you posted not just any text file as if it is not comma separated then code would need to be modified.
gowflow
0
 

Author Comment

by:murbro
ID: 39711387
Thank you both for the answer and the qualification
0
 

Author Comment

by:murbro
ID: 39718647
Thanks
0

Featured Post

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!

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 tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

762 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