+ Reply to Thread
Results 1 to 11 of 11

Loop through folder with txt files, import them and save as xls file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Loop through folder with txt files, import them and save as xls file

    Hi

    I am trying to write a macro that will loop through my folder of txt files, import them that will use the import feature and delimit it, then save as an xls file.

    So far, I have only been able to use the following, but it only allows me to import one file (and I know the folder path, I want to avoid choosing the folder).

    It also adds many quotation marks ("") when I only want to import using tab delimiter and comma.

    Can anyone help me with this?

    Sub ImportTextFile()
    Dim fName As String
    
    fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If fName = "False" Then Exit Sub
    
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
            Destination:=Range("$A$1"))
                .Name = "sample"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 437
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierNone
                .TextFileConsecutiveDelimiter = True
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileOtherDelimiter = "" & Chr(10) & ""
                .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
                   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
        End With
    End Sub

  2. #2
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    I am trying to write a macro that will loop through my folder of txt files
    Q - every file?

    import them
    Q - into one sheet\ each text file to different sheets\different workbooks?

    It also adds many quotation marks ("") when I only want to import using tab delimiter and comma
    please attach a sample typical text file to make testing possible - easier to play with the real data

    to attach a file
    - click on Reply
    - Click on GoAdvanced
    - look below fo ManageAttachments etc

    thanks
    Click *Add Reputation to thank those who helped you. Ask if anything is not clear

  3. #3
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Hi

    yes it is every file to loop through in the folder. They are all txt files.

    They should come into separate workbooks, i.e. import one txt file into one workbook (one sheet) then save it as xls (same file name), close and open next file and so on. They all need to be saved in the same folder that the txt files are currently in.

    I hope that explains it properly.

    Attached is a small sample. The way I import using the wizard is to use delimited, comma, general, finish.
    Attached Files Attached Files
    Last edited by maym; 10-03-2017 at 07:20 PM.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Loop through folder with txt files, import them and save as xls file

    Hi maym,

    If you were using Power Query, this would be an easy problem. See the attached. Is this what you want? Or will you only accept a VBA answer?

    SampleAnswer.xlsx
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Hi Marvin

    thanks for your reply. I am looking to do this through VBA if possible?

  6. #6
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    I have found something that looks like when executed, will give me close to the desired result:

    Sub Import()
       Dim FS As New FileSearch
       Dim File
       Dim Wb As Workbook
       
       With FS
         .LookIn = "C:\Users\L\Desktop\"
         .FileName = "*.txt"
         .SearchSubFolders = True
         
         If .Execute = 0 Then
           MsgBox "No files found"
           Exit Sub
         End If
         
         Application.DisplayAlerts = False
         
         For Each File In .FoundFiles
           
           Set Wb = Workbooks.Open(File, Format:=6, Delimiter:=vbTab)
           Wb.SaveAs Left(File, InStrRev(File, ".") - 1), xlWorkbookDefault
           
           Wb.Close
         Next
       End With
    
    End Sub
    However I get a 'compile error: invalid use of new keyword' on this line:

    Dim FS As New FileSearch
    Can anyone assist with this please?

  7. #7
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    I have managed to put together the following. However as shown in the sample txt file, the dates should show as 7/1/2013 (1 Jan) but when I re-open the file, they show as 1/7/2013 (1 July).

    How can I solve for this??

    Sub Import()
       'DECLARE AND SET VARIABLES
        Dim wbk As Workbook
        Dim FileName As String
        Dim Path As String
        Dim ws As Excel.Worksheet
    
            Path = "C:\Users\L\Desktop\"
            FileName = Dir(Path & "*txt")
            
       
                Do While Len(FileName) > 0  'IF NEXT FILE EXISTS THEN
                
            
                    Workbooks.OpenText FileName:="C:\Users\L\Desktop\" & FileName, Origin _
                    :=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote _
                    , ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=False, Comma:=True _
                    , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
                    Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), Array(9, 1), _
                    Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1), Array(14, 1), Array(15, 1), Array( _
                    16, 1), Array(17, 1), Array(18, 1)), TrailingMinusNumbers:=True                            
                                   
                                
                 Application.DisplayAlerts = False
                  ActiveWorkbook.SaveAs Path & Left(FileName, Len(FileName) - 3) & "xls", FileFormat:=xlNormal
                   
           
           ActiveWorkbook.Close True
           FileName = Dir
                Loop
    
    End Sub
    Last edited by maym; 10-05-2017 at 07:19 AM.

  8. #8
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Anybody know how I can solve this???

  9. #9
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    Your thread somehow disappeared off my subscribed threads list - so has not been on my radar - I will have a look later today/tomorrow and update the thread after that
    kev

  10. #10
    Forum Contributor
    Join Date
    10-12-2011
    Location
    Sydney
    MS-Off Ver
    Excel 2016 in 2024
    Posts
    857

    Re: Loop through folder with txt files, import them and save as xls file

    Quote Originally Posted by kev_ View Post
    Your thread somehow disappeared off my subscribed threads list - so has not been on my radar - I will have a look later today/tomorrow and update the thread after that
    kev
    perfect, thanks so much kev

  11. #11
    Forum Expert
    Join Date
    11-22-2016
    Location
    Cornwall,UK
    MS-Off Ver
    office 365
    Posts
    4,240

    Re: Loop through folder with txt files, import them and save as xls file

    Unfortunately with Workbooks.OpenText, Excel brings in date&time values as numbers interpreted via application default - and Excel's native tongue is American!
    (As far as I know) there is no way to fix the code to allow Workbooks.OpenText to be used and get the dates correct

    Therefore forced to import the data, where everything is brought in as text
    - this method adds a connection to the text file (not intending to update later - so connection is removed by VBA)
    - the date & time values are now text (does not matter unless you need to perform calculations or sort)
    - if that is a problem then just need to a add a function to convert date&time text to values (do you need this?)
    - I noticed that columns L and N are better treated as numbers and added a routine to convert those values to numbers
    - that simple routine unfortunately does not work with the date&time text (more precisely - it works but gets the answer wrong )
    - other columns left as text
    - your original VBA (post#7) was creating xls files, whereas this creates xlsx files (this should not be an issue given that you are using Excel 2010)


    Test in attached file with {CTRL} k

    Place this code in a general module:
    Sub ImportIt()
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        Dim sourcePath As String, destnPath As String, FileName As String, SaveName As String, wb As Workbook
        
        sourcePath = "C:\Users\L\Desktop\"
        destnPath = sourcePath
        FileName = Dir(sourcePath & "*txt")
            
        Do While Len(FileName) > 0
            Set wb = Workbooks.Add
            Call ImportTxt(sourcePath & FileName)
            Call DeleteConnections
            Call FixNumbers
                
            SaveName = Left(FileName, InStrRev(FileName, ".") - 1)
            Application.DisplayAlerts = False
                wb.SaveAs (destnPath & SaveName)
                wb.Close False
            Application.DisplayAlerts = True
            FileName = Dir
        Loop
    
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
    End Sub
    Private Sub ImportTxt(strFile As String)
        'initialising data type array bigger than required is lazy but it works
        Dim ws As Worksheet, cdtArray(1 To 50) As Long, i As Long
        Set ws = ActiveSheet
            For i = 1 To UBound(cdtArray)
                cdtArray(i) = 2
            Next i
    
        With ws.QueryTables.Add(Connection:="TEXT;" & strFile, Destination:=ws.Range("A1"))
            .TextFileParseType = xlDelimited
            .TextFileCommaDelimiter = True
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileColumnDataTypes = cdtArray
            .Refresh
        End With
    End Sub
    Private Sub FixNumbers()
        'required because all values imported as text
        Dim colArray(), C
        colArray = Array("L", "N")
        
        For Each C In colArray
            With Columns(C)
                .Select
                .Copy
                .PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd
                .NumberFormat = "0.00"
            End With
        Next C
        Range("A1").Select
    End Sub
    Private Sub DeleteConnections()
        'connection no longer required
        Do While ActiveWorkbook.Connections.Count > 0
            ActiveWorkbook.Connections.Item(ActiveWorkbook.Connections.Count).Delete
        Loop
    End Sub
    Attached Files Attached Files
    Last edited by kev_; 10-06-2017 at 02:47 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Loop through files in folder, email file to listed recipient of file name = cell value
    By Groovicles in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-04-2017, 03:41 PM
  2. [SOLVED] Import data from files in folder with a given word/text in the file name
    By runnerD2016 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2016, 11:20 AM
  3. [SOLVED] Loop through multiple files, run macro, then save as Excel workbook in a different folder
    By Peter Kallio in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 08-26-2013, 11:42 PM
  4. Replies: 0
    Last Post: 04-16-2012, 10:57 AM
  5. Save File As Cell Value (loop through folder)
    By uncleslinky in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-22-2012, 04:02 AM
  6. Replies: 17
    Last Post: 12-06-2011, 11:50 AM
  7. Import all access files from a folder to a single excell file
    By wali in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-08-2009, 05:04 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1