Solved

Excel VBA macro that can import any text file to Excel

Posted on 2013-12-11
6
772 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:Murray Brown
[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 52

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 31

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:Murray Brown
ID: 39711387
Thank you both for the answer and the qualification
0
 

Author Comment

by:Murray Brown
ID: 39718647
Thanks
0

Featured Post

Technology Partners: 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…
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.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

635 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