+ Reply to Thread
Results 1 to 3 of 3

Use Dir in If statement to delete old file, save new file

  1. #1
    Registered User
    Join Date
    12-06-2014
    Location
    Arizona
    MS-Off Ver
    2011
    Posts
    20

    Use Dir in If statement to delete old file, save new file

    I am trying to use and If statement to:

    1. check and see if an old copy of a file exists in a folder,
    2. and if so delete it,
    3. then check to see if a new copy of a file exists in the same folder,
    4. and if not save a copy.

    I keep getting a Run-Time 13 error, Type Mismatch on the Dir statement, and endless searches have not yielded any results. Below I have pasted a sample of the code that I have tried (I have tried many different configurations with the Dir declaration, none have worked obviously). I have tried using variables where the full file path and file name are assigned to them, and also cutting them up into one variable that stores the path, and another variable that stores the file name.

    Here is a sample:

    Please Login or Register  to view this content.
    Thank you

  2. #2
    Registered User
    Join Date
    04-14-2010
    Location
    NZ
    MS-Off Ver
    2007,2010
    Posts
    86

    Re: Use Dir in If statement to delete old file, save new file

    I'm guessing that the type mismatch is because Dir returns a string not true/false/empty

    Please Login or Register  to view this content.
    I've run into time where this hasn't picked up an existing file, so I tend to use a temporary variable to store the result of the DIR, and check that.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    12-06-2014
    Location
    Arizona
    MS-Off Ver
    2011
    Posts
    20

    Re: Use Dir in If statement to delete old file, save new file

    Had to go the temporary variable storage route, but it worked like a charm, thanks!

+ 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. Save as macro that specifies file type, file location and takes file name from three cells
    By ExcelFailure in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-13-2013, 10:09 PM
  2. Replies: 0
    Last Post: 01-27-2013, 12:13 PM
  3. [SOLVED] Need help to save file to new folder and delete old file using same file name
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-14-2012, 11:16 PM
  4. How to: Open file, format data, save file, close file and repeat.
    By thexeber in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-11-2010, 12:56 PM
  5. Save file, create .pdf, (Now delete .xls file)
    By daniels012 in forum Excel General
    Replies: 4
    Last Post: 03-30-2007, 05:14 PM

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