You are here:

Excel/(Import text file and Text2Column) in Loop

Advertisement


Question
Hi Sir,

How are you doing?

Sir, I need your help.

I have many files in one folder and in the same folder I have excel file. What I want to do is to write a macro which can open .txt files in the same particular folder which has my excel macro file one by one using (Data >> From Text) Paste it in A2...... then do simple "Text to columns" >> Delimited >> Delimiters (Tab) [Default options of Text to column".

A1 should have file name which it opened.

I've recorded the macro to do this...  But I donno how to loop it, so that it can pick all the .txt files in that folder and save it in different sheets one by one and have text file name in A1 column. Could you please please please please help me with it sir.

It will solve veryyyyyyyyyy big problem of mine. Please need your help sir.

This is what I have recorded

Sub Macro1()
   With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;E:\My_Files\TextFileName.txt" _
       , Destination:=Range("$A$2"))
       .Name = "TextFileName"
       .FieldNames = True
       .RowNumbers = False
       .FillAdjacentFormulas = False
       .PreserveFormatting = True
       .RefreshOnFileOpen = False
       .RefreshStyle = xlInsertDeleteCells
       .SavePassword = False
       .SaveData = True
       .AdjustColumnWidth = True
       .RefreshPeriod = 0
       .TextFilePromptOnRefresh = False
       .TextFilePlatform = 65001
       .TextFileStartRow = 1
       .TextFileParseType = xlDelimited
       .TextFileTextQualifier = xlTextQualifierDoubleQuote
       .TextFileConsecutiveDelimiter = False
       .TextFileTabDelimiter = True
       .TextFileSemicolonDelimiter = False
       .TextFileCommaDelimiter = False
       .TextFileSpaceDelimiter = False
       .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
       .TextFileTrailingMinusNumbers = True
       .Refresh BackgroundQuery:=False
   End With
End Sub

Many thanks in advance.

Regards,
Sirajuddin

Answer
Sirajuddin,

I can't test it, but I would use code like this:

Sub Openfiles()
Dim sPath As String, sName As String, sName1 As String
Dim iloc As Long
sPath = ThisWorkbook.Path
If Right(sPath, 1) <> "\" Then
 sPath = sPath & "\"
End If
sName = Dir(sPath & "*.txt")
Do While sName <> ""
 iloc = InStr(1, sName, ".", vbTextCompare)
 If iloc <> 0 Then
   sName1 = Left(sName, iloc - 1)
 Else
   sName1 = sName
 End If
 Worksheets.Add After:=Worksheets(Worksheets.Count)
   ActiveSheet.Name = sName1
   With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;" & sPath & sName _
       , Destination:=Range("$A$2"))
       .Name = sName1
       .FieldNames = True
       .RowNumbers = False
       .FillAdjacentFormulas = False
       .PreserveFormatting = True
       .RefreshOnFileOpen = False
       .RefreshStyle = xlInsertDeleteCells
       .SavePassword = False
       .SaveData = True
       .AdjustColumnWidth = True
       .RefreshPeriod = 0
       .TextFilePromptOnRefresh = False
       .TextFilePlatform = 65001
       .TextFileStartRow = 1
       .TextFileParseType = xlDelimited
       .TextFileTextQualifier = xlTextQualifierDoubleQuote
       .TextFileConsecutiveDelimiter = False
       .TextFileTabDelimiter = True
       .TextFileSemicolonDelimiter = False
       .TextFileCommaDelimiter = False
       .TextFileSpaceDelimiter = False
       .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
       .TextFileTrailingMinusNumbers = True
       .Refresh BackgroundQuery:=False
   End With
 sName = Dir()
Loop
End Sub

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.