+ Reply to Thread
Results 1 to 8 of 8

Looping through all files in a folder - save as xlsx

  1. #1
    Registered User
    Join Date
    07-21-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    4

    Looping through all files in a folder - save as xlsx

    Apologies for a (I think) very basic question. I know this has been answered many times in many places, but the solutions I've found are rather sophisticated, well beyond my understanding at this stage.

    I am looking to save all csv files in a folder in xlsx format. I have a macro (in a workbook that is open) that does this fine for a file with known name & extension. I'd like to avoid just copying this command many times and changing the name.

    The command I have is

    Please Login or Register  to view this content.
    my files are all in the format output#.csv (where # is just a number). There are no other csv files in the folder, so I don't have to worry about matching the textstring - just want to loop through all the csv files. The number of files in the folder varies, so if I can avoid using a for loop and setting a string like outputX.csv where I iterate on X that'd be nice, but if necessary can do that.

    I'm very new to VBA and needing a quick solution to a problem, so I'm not looking for anything sophisticated, just the most straightforward way to create the loop, either through all files (I've seen things like Do While name <> "" but haven't managed to adapt to get this working - yet) or set the string based on an iterator that I can manually set each time if necessary. At the moment I'm looking for something I can understand rather than a faster/better way of doing things (I can get to that when I have some time to start learning this programming properly).

    Thanks

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

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

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

    Please Login or Register  to view this content.
    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.

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,523

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

    Here's my attempt:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    07-21-2013
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    4

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

    Thanks AlphaFrog, especially for taking the time to explain each of the steps - it's helped explain what I wasn't understanding in other solutions I'd seen, and I've even managed to make small changes for another problem I had (small achievement I know)

    thanks also Robert, I'll look through your solution as well when I have a little more time.

    Emse

  5. #5
    Registered User
    Join Date
    10-29-2012
    Location
    Mount Pleasant, South Carolina
    MS-Off Ver
    Excel 2010
    Posts
    2

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

    AlphaFrog - Your solution works great for me, with one exception. It is saving the excel files into my January folder instead of the originating April folder. Please advise.

    Sub Convert_CSV_Files()


    Application.DisplayAlerts = False

    'Declare Variables
    Dim strPath As String, strFile As String, counter As Long

    strPath = "\\tundra\departments\Retail_Managers\Budget Reports\EMAILED REPORTS\2014\APRIL\" '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 & "Budget*.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(strFile) 'Open the csv file
    strFile = Replace(strFile, ".csv", ".xlsx") 'Replace file extension in the file name
    .SaveAs 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

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

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

    You changed this line and deleted the path for some reason.


    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    10-29-2012
    Location
    Mount Pleasant, South Carolina
    MS-Off Ver
    Excel 2010
    Posts
    2

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

    That fixed it. Thanks!!!

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

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

    You're welcome Andrea, and well done on searching for a solution first.

+ 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. 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