+ Reply to Thread
Results 1 to 9 of 9

Convert all .txt files to .xls in folder

Hybrid View

  1. #1
    Registered User
    Join Date
    10-18-2011
    Location
    Redondo Beach, CA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Convert all .txt files to .xls in folder

    Hi all,

    I'm trying to write a VBA macro that opens all .txt files in a folder and saves them as .xls with the same name. I've found some code online that has some of the same functionality but they use Application.Filesearch which is no longer available on Excel 2010.

    I found an example using the Dir function but am having trouble getting it to work.

    Thanks for any help you can offer.


    Sub LoopAllFiles()
    
    sPath = "S:\Test loop macro\files"
    
    sDir = Dir$(sPath & "*.txt", vbNormal)
    
    Do Until LenB(sDir) = 0
    
    Set oWB = Workbooks.Open(sPath & sDir)
    
        ActiveWorkbook.SaveAs , FileFormat:= _
            xlOpenXMLWorkbook, CreateBackup:=False
    
    oWB.Close
    
    sDir = Dir$
    
    Loop
    End Sub
    Last edited by natalie1230; 10-18-2011 at 02:46 PM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert all .txt files to .xls in folder

    Hi Natalie, welcome to the forum.

    Be sure to read through the Forum Rules so you can use and follow them effectively. For instance, you'll need to EDIT that post above and put code tags around that code you used. (Like shown in my signature)
    Sub MyMacro()
       Code
          Code
       Code
    End Sub
    Thanks for fixing that, we can offer some ideas on this.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    10-18-2011
    Location
    Redondo Beach, CA
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Convert all .txt files to .xls in folder

    Thanks, Jerry!

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Convert all .txt files to .xls in folder

    Hi Natalie

    You need a back slash in this line of code
    sPath = "S:\Test loop macro\files\"
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

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

    Re: Convert all .txt files to .xls in folder

    Cross-posted to: http://www.mrexcel.com/forum/showthread.php?t=586389

    Read more about cross-posting here: http://www.excelguru.ca/node/7

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert all .txt files to .xls in folder

    'CSVs To Workbooks
    I have a macro that may be "ready to use" for converting all files of a specified type, like CSV or TXT, in a specified folder into Excel files. There's a workbook included to run the macro from where you indicate your preferences on the SETUP sheet.
    CSVs to Workbooks
    (or other file types to Excel Workbooks)

  7. #7
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Convert all .txt files to .xls in folder

    The same functionality of JB's code but somewhat more compact:

    Sub CSVsToWorkbooks()
     Application.ScreenUpdating = False
     Application.DisplayAlerts = False
      
     c00 = "E:\OF\"
     c01 = Dir(c00 & "*.txt")
    
     Do While c01 <> ""
      With Workbooks.Open(c00 & c01)
       .SaveAs c00 & .Sheets(1).Name & ".xls", xlNormal
       .Close
      End With
      c01 = Dir
     Loop
     
     if dir(c00 & "old",16)="" then MkDir c00 & "old\"
     CreateObject("scripting.filesystemobject").movefile c00 & "*.txt", c00 & "\old"
    End Sub
    PS. @JB

    I don't like 'killing'



  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Convert all .txt files to .xls in folder

    PS. @JB

    I don't like 'killing'
    The file is flexible and entry-level editable, more aimed at ease of use and readability. Killing (deleting) the source file(s) when done is only one of the options offered. Your dislike of killing is understandable, others would simply opt to not use that option if they feel the same way. The benefit of having the option, though, should be evident.

    On the other hand, I do pick up things from your brevity code, so keep it up!

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Convert all .txt files to .xls in folder

    @JB

    "Killing' wasn't so much on your VBA code, but in a more pacifistic sense.

    I posted my answer primarily to draw your attention to:

    - the name that is being given automatically to the first sheet
    - the use of that name to name the file
    - the method movefile (using *)

    Considering your post, you noticed

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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