+ Reply to Thread
Results 1 to 3 of 3

Macro for File Management Keeps Adding a Subdirectory!

Hybrid View

  1. #1
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Macro for File Management Keeps Adding a Subdirectory!

    Hi everyone,

    I have a new Macro (written in VBA 7.1) which is supposed to do the following:
    • In the same directory as the executing Excel spreadsheet, create a new directory called "SAVED_FILES" (if the directory doesn't already exist)
    • For each Tab/Worksheet in the Spreadsheet, perform a "Save As" on the Tab/WS, using the tab's name as the new ___.csv filename. Each CSV file should be placed in "SAVED_FILES". If another file is already in the subdirectory with the same name, that file should be overwritten.


    E.g., if I have spreadsheet with TabA, TabB, and TabC, then after the Macro is run, I should see this in my current directory:

       -  MyExcel.xlsx
       -  SAVED_FILES
               - TabA.csv
               - TabB.csv
               - TabC.csv
    My code is almost there. The first time it runs, it works perfectly. But then the second time it runs, it does this:

       -  MyExcel.xlsx
       -  SAVED_FILES
               -  SAVED_FILES
                     - TabA.csv
                     - TabB.csv
                     - TabC.csv
               - TabA.csv
               - TabB.csv
               - TabC.csv
    That's no good. Each time the Macro runs, adds a new "SAVED_FILES" directory in the previous "SAVED_FILES" directory.

    Below is the code of the full Macro. (Apologies for the length!) I'll have some observations below:

    Sub SaveWorksheetsAsCSV()
        Dim ws As Worksheet
        Dim currentPath As String
        Dim csvFilename As String
        Dim savedFilesPath As String
        Dim originalWorksheet As Worksheet
    
        ' Get the current path of the workbook
        currentPath = ThisWorkbook.Path
    
        ' Create the SAVED_FILES subdirectory if it doesn't exist
        savedFilesPath = currentPath & "\SAVED_FILES"
        If Dir(savedFilesPath, vbDirectory) = "" Then
            MkDir savedFilesPath
        End If
    
        ' Store the currently active worksheet
        Set originalWorksheet = ActiveSheet
    
        ' Loop through each worksheet in the workbook
        For Each ws In ThisWorkbook.Worksheets
            ' Activate the worksheet to ensure its contents are saved correctly
            ws.Activate
    
            ' Set the CSV filename based on the worksheet's name
            csvFilename = savedFilesPath & "\" & ws.Name & ".csv"
    
            ' Save the worksheet as a CSV file
            ws.SaveAs Filename:=csvFilename, FileFormat:=xlCSV, CreateBackup:=False
        Next ws
    
        ' Restore the originally active worksheet
        originalWorksheet.Activate
    
        ' Inform the user that the process is complete
        MsgBox "All worksheets have been saved as CSV files in the SAVED_FILES subdirectory.", vbInformation
    
    End Sub
    From debugging, I'm pretty sure the problem is here:

        ' Create the SAVED_FILES subdirectory if it doesn't exist
        savedFilesPath = currentPath & "\SAVED_FILES"
    With every iteration of the Macro, the `currentPath` variable seems to be the `savedFilesPath` variable from the previous iteration. This is why a new subdirectory is appended to the string every time.

    But why? I thought that when the Macro exits, no variable data is saved. Is there a way to clear this? Or is the problem elsewhere? Thank you.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hi, try this !


    Quote Originally Posted by redapplesonly View Post
    I have a new Macro (written in VBA 7.1) which is supposed to do the following
    Very not what it does so just removing the bad & the useless and adding the appropriate Excel logic like this VBA demonstration :

    PHP Code: 
    Sub Demo1()
        
    Dim P$, Ws As Worksheet
            P 
    ThisWorkbook.Path "\SAVED_FILES\":  If Dir(P, 16) <> "." Then MkDir P
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
        For Each Ws In ThisWorkbook.Worksheets
            Ws.Copy
            ActiveWorkbook.SaveAs P & Ws.Name, 6
            ActiveWorkbook.Close
        Next
            .Speech.Speak "
    Done!", True
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  3. #3
    Registered User
    Join Date
    01-17-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    25

    Re: Hi, try this !

    Yes, this worked perfectly!!! Thank you, this was great! 10,000 Kudos and Karma to you!

+ 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] Find pdf file within subdirectory to attach to email
    By clorodet20607 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 04-11-2021, 12:57 AM
  2. can't find file with macro in current subdirectory
    By xi603 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-28-2017, 06:47 PM
  3. Replies: 0
    Last Post: 01-04-2017, 12:07 AM
  4. [SOLVED] List all files in Directory and Subdirectory with File Property Details
    By Green Crocodile in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-26-2016, 09:59 PM
  5. Replies: 8
    Last Post: 11-12-2012, 02:39 PM
  6. [SOLVED] Return most recent file in subdirectory with out using FileSearch
    By Enohp Aikon in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 10-03-2005, 07:05 PM
  7. File management
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-18-2005, 04:07 AM

Tags for this Thread

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