Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA macro that can import any text file to Excel

Posted on 2013-12-11
6
Medium Priority
?
814 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

715 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