Results 1 to 8 of 8

Looping through all files in a folder - save as xlsx

Threaded View

  1. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Looping through all files in a folder - save as xlsx

    Try this. Change the file path and file name pattern to suit.

    Sub Convert_CSV_Files()
    
        'Declare Variables
        Dim strPath As String, strFile As String, counter As Long
        
        strPath = "C:\Users\...\Analysis\"      'File folder path
        
        'File name: "output*.csv"   The asterisk is a wild card
        'In this case, all file names start with "output" and end with ".csv"
        strFile = Dir$(strPath & "output*.csv") 'This gets the first file name (if any) that matches the patterm.
        
        Do While Len(strFile)   'Loop while the file name is not blank
            
            With Workbooks.Open(strPath & strFile)              'Open the csv file
                strFile = Replace(strFile, ".csv", ".xlsx")     'Replace file extension in the file name
                .SaveAs strPath & strFile, xlOpenXMLWorkbook    'Save as an .xlsx" file
                .Close                                          'Close the .xlsx file
            End With
            
            counter = counter + 1   'Count the files converted
            
            'Get the next csv file name.
            'Uses the same pattern as the Dir function above
            'Returns a blank string after the last csv file.
            strFile = Dir$
            
        Loop    'will stop when no more .csv files to open
        
        MsgBox counter & " file(s) converted. ", vbInformation, "Conversions Complete"
        
    End Sub
    Last edited by AlphaFrog; 07-23-2013 at 10:10 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Looping through a folder of files in VBA
    By lizzo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-13-2013, 05:29 PM
  2. [SOLVED] Macro to save file as XLSX in the same folder from where it originated
    By balandri in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-07-2013, 02:28 PM
  3. How to add the columns data of several xlsx files of a folder in another xlsx file
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-25-2012, 04:29 AM
  4. Save all .xlsx files in a directory to .xls
    By paul00 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-24-2012, 08:05 PM
  5. Macro to open & close mutiple xlsx files in folder
    By bernard.x in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-20-2012, 06:18 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