+ Reply to Thread
Results 1 to 7 of 7

Automatically Convert Txt files in a folder to Excel

Hybrid View

  1. #1
    Registered User
    Join Date
    08-24-2007
    MS-Off Ver
    14.0
    Posts
    69

    Automatically Convert Txt files in a folder to Excel

    Good Day!

    I have text files that I have to save as Excel for another function to work. Is there a way to automate this, such as to create a folder that through VBA or Macro would convert all files in that folder to Excel?

    I am just trying to reduce steps in the current process.. Any help or direction would be greatly appreciated..

    Thanks.

    -Tommy

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Automatically Convert Txt files in a folder to Excel

    Is it a delimited file? IF so, maybe attach a simple obfuscated file so that we can best help. Would it become an XLSX file?

  3. #3
    Registered User
    Join Date
    08-24-2007
    MS-Off Ver
    14.0
    Posts
    69

    Re: Automatically Convert Txt files in a folder to Excel

    Thanks for the reply.. The file is delmited... I would like it to become an XLSX file..

    Thanks.

    -Tommy
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    11-21-2016
    Location
    Emmen, The Netherlands
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    19

    Re: Automatically Convert Txt files in a folder to Excel

    If the txt files ar delimited semicolon, the following code can be usefull.


    Sub convert()
        Dim strDir As String
        Dim strFile As String
        Dim intPos As Integer
        
        strDir = ActiveSheet.Cells(1, 1).Value
        
        If Left(strDir, 1) <> "\" Then strDir = strDir & "\"
        intPos = Len(strDir)
        strFile = Dir(strDir & "*.txt")
        Do While Len(strFile) > 0
            Workbooks.OpenText strDir & strFile, , , xlDelimited, , , , True
            ActiveWorkbook.SaveAs strDir & Left(ActiveWorkbook.Name, Len(strFile) - 4) & ".xlsx", FileFormat:=xlOpenXMLWorkbook
            strFile = Dir
        Loop
        
    End Sub
    HTH

  5. #5
    Registered User
    Join Date
    08-24-2007
    MS-Off Ver
    14.0
    Posts
    69

    Re: Automatically Convert Txt files in a folder to Excel

    Thanks for the code.. please advise on where to paste it..

    Very much appreciate the assistance..

    -Tommy

  6. #6
    Registered User
    Join Date
    11-21-2016
    Location
    Emmen, The Netherlands
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    19

    Re: Automatically Convert Txt files in a folder to Excel

    Made a few changes in the code:
    1. your file is tab-delimited, so I changed te paramater after the opentext method to tab:=true
    2. After saving the file as an xlsx-file it has to be closed obviously... my bad

    Sub convert()
        Dim strDir As String
        Dim strFile As String
        Dim intPos As Integer
        
        strDir = ActiveSheet.Cells(1, 1).Value
        
        If Left(strDir, 1) <> "\" Then strDir = strDir & "\"
        intPos = Len(strDir)
        strFile = Dir(strDir & "*.txt")
        Do While Len(strFile) > 0
            Workbooks.OpenText strDir & strFile, , , xlDelimited, , , Tab:=True
            ActiveWorkbook.SaveAs strDir & Left(ActiveWorkbook.Name, Len(strFile) - 4) & ".xlsx", FileFormat:=xlOpenXMLWorkbook
            ActiveWorkbook.Close False
            strFile = Dir
        Loop
        
    End Sub
    The code can be placed in a xlsm-file. Select the sheet with the target-directory in cell A1. Right-click on the sheet tab, click on add code and paste the code.
    Run the code while the sheet with the target-directory is selected by pressing [alt]+[F8] or create an object to which you connect the macro.

    See attached file for an example.

    ---
    Good luck
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-21-2016
    Location
    Emmen, The Netherlands
    MS-Off Ver
    MS Office 365 ProPlus
    Posts
    19

    Re: Automatically Convert Txt files in a folder to Excel

    Dear Tommy,

    I would appreciate a response to my last post. Did my example help you?

    Ton.

+ 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. Convert csv files in folder to xlsm
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-13-2015, 09:49 PM
  2. [SOLVED] How to convert all word files stored in the folder to PDF automatically?
    By mso3 in forum Word Programming / VBA / Macros
    Replies: 13
    Last Post: 11-02-2015, 12:01 PM
  3. Automatically make new excel-files (and save in specific folder on my computer)
    By Josvanderwaaij in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 08-21-2014, 06:32 AM
  4. Convert all .txt files to .xls in folder
    By natalie1230 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-19-2011, 03:50 AM
  5. Convert all txt files in Folder to csv
    By nms2130 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-07-2011, 11:14 AM
  6. convert date format to text in all excel files in a folder
    By farrukh in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-31-2011, 01:12 PM
  7. Open rtf files from a folder and convert them to pdf
    By TANATOS in forum Word Programming / VBA / Macros
    Replies: 0
    Last Post: 07-08-2009, 01:03 PM

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