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

x
?
Solved

Excel VBA macro that can import any text file to Excel

Posted on 2013-12-11
6
Medium Priority
?
851 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
6 Comments
 
LVL 34

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 53

Accepted Solution

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

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
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 will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

972 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