+ Reply to Thread
Results 1 to 3 of 3

Macro for File Management Keeps Adding a Subdirectory!

  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:

    Please Login or Register  to view this content.
    My code is almost there. The first time it runs, it works perfectly. But then the second time it runs, it does this:

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

    Please Login or Register  to view this content.
    From debugging, I'm pretty sure the problem is here:

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