+ Reply to Thread
Results 1 to 2 of 2

Save files in a different format from a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    04-09-2015
    Location
    delhi
    MS-Off Ver
    7
    Posts
    15

    Save files in a different format from a folder

    Hello

    I have a folder which has multiple Excel files in it (extensions can differ).

    I want to open this folder, loop through each file, and save it as a binary file in the same folder. Also, I would like to keep the original name of the file for the new binary file.

    I have the following code, which I have written. However, I am not able to capture the same file name and progress further.

    Can someone help me please?

    Sub cons()

    Dim myfile As String, myfolder As String, z As String

    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    'create a new workbook and rename it to store the name of the workbooks in the folder
    Workbooks.Add.SaveAs "C:\Users\skhur1\Desktop\Named workbook", FileFormat:=50

    'storing folder path
    myfolder = "C:\Users\skhur1\Desktop\New folder\"

    'storing files path
    myfile = Dir(myfolder)

    'loop to open the folder and loop through the files saved in it.

    Do While Len(myfile) > 0

    'opening each file one by one
    Workbooks.Open (myfolder & myfile)

    'Saving the name of each file in Cell A1. This step can be skipped, if there is another way..
    Workbooks("Named workbook").Sheets(1).Range("a1") = ActiveWorkbook.Name

    'Trying to store the name of each file in a variable z

    z = Workbooks("Named workbook").Sheets(1).Range("a1").Value

    'activating the workbook with the name stored above
    Windows(z).Activate

    ActiveWorkbook.SaveAs (myfolder & z), FileFormat:=50

    ActiveWorkbook.Close

    myfile = Dir()

    Loop

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    End Sub

    On the second loop, it gives me an error. I'm attaching the screenshot as well.
    Attached Files Attached Files

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

    Re: Save files in a different format from a folder

    Sub cons()
        
        Dim myfile As String, myfolder As String, counter As Long
        
        Application.DisplayAlerts = False
        Application.ScreenUpdating = False
        
        'create a new workbook and rename it to store the name of the workbooks in the folder
        'Workbooks.Add.SaveAs "C:\Users\skhur1\Desktop\Named workbook", FileFormat:=50
        
        'storing folder path
        myfolder = "C:\Users\skhur1\Desktop\New folder\"
        
        'storing files path
        myfile = Dir(myfolder & "*.xls*")
        
        'loop to open the folder and loop through the files saved in it.
        
        Do While Len(myfile) > 0
        
            If LCase(Right(myfile, 4)) <> "xlsb" Then
        
                'opening each file one by one
                With Workbooks.Open(myfolder & myfile)
                    
                    .SaveAs myfolder & Left(.Name, InStrRev(.Name, ".") - 1), FileFormat:=50
                    
                    .Close
                    
                    counter = counter + 1
                    
                End With
                
            End If
        
            myfile = Dir
        
        Loop
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
        MsgBox counter & " files resaved. "
        
    End Sub
    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.

+ 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] Looping through all files in a folder - save as xlsx
    By Emse_ in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-18-2014, 02:15 PM
  2. [SOLVED] macro to open all files in a folder, save them, then close them
    By hopefulhart in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-27-2013, 06:40 PM
  3. Macro to create a folder and then save all files in that folder
    By gokzee in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-27-2013, 01:49 PM
  4. Run a folder with xls fles and save as txt files
    By soli004 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-28-2012, 09:52 AM
  5. VBA to create folder and then save multiple files In
    By ZeDoctor in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-22-2012, 01:43 PM
  6. [SOLVED] Cannot save excel files to a folder on a network
    By Robbie in forum Excel General
    Replies: 1
    Last Post: 06-09-2006, 02:30 AM
  7. Save copy of folder with linked files
    By SOS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-01-2006, 03:15 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