+ Reply to Thread
Results 1 to 5 of 5

Delete excel files in a given folder

  1. #1
    Registered User
    Join Date
    04-12-2013
    Location
    Caracas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Delete excel files in a given folder

    Hi,

    I have macro that creates a new workbook based on a sample sheet within the workbook containing the macro. The macro names the file with a correlative number (based on the value of a cell that increases by one everytime a new workbook is created). So, for example, the first file created will have the name "name 1.xlsx", the second "name 2.xlsx", the third, "name 3.xlsx" and so on.

    I'd like to have a separate macro that deletes the last workbook created so that if the user made a mistake when creating a file, say file "name 13.xlsx", he could run this macro and have it deleted. I can't figure out how to tell the macro that the name of the file is "name (cell value).xlsx". "Cell value" being the correlative number of the last workbook created. Is this even possible?

    Thanks in advanced
    Last edited by adrianrff; 08-21-2013 at 03:24 PM.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Delete excel files in a given folder

    please, paste here your code
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    04-12-2013
    Location
    Caracas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete excel files in a given folder

    Hi, patel45.

    I don't have a code yet for deleting the file. The macro I want would be separate from the macro that creates the file. All I want it to do is look for the last file created (using the correlative number found in cell A2) and delete it.

    If it helps. This is the code that creates the file:

    Please Login or Register  to view this content.
    I know it's a crazy looking and messy code. I really have no training in VBA at all, all i know, I've learned along the way.

    As I said, I need the macro that deletes the file to be separate from this one. What I really don't know how to do is give the macro a varying file name (based on the value of the cell [A2] containing the correlative number)

    Thanks
    Last edited by adrianrff; 08-21-2013 at 03:04 PM.

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,197

    Re: Delete excel files in a given folder

    Hi, adrianrff,

    maybe something like this (you need to change the name of the sheet and range as well as the location to suit):
    Please Login or Register  to view this content.
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    04-12-2013
    Location
    Caracas
    MS-Off Ver
    Excel 2007
    Posts
    8

    Re: Delete excel files in a given folder

    Thanks, HaHoBe!

    Worked like a charm.

    I struggled at first because I'm not familiar with ThisWorkbook.Path format. I failed to realise that ThisWorkbook.Path meant "This workbook's path", duh! I kept typing the whole path and, of course, it couldn't find the file.

    Thanks again.
    Last edited by adrianrff; 08-21-2013 at 03:26 PM.

+ 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. Macro to delete csv files in downloads folder
    By prontrad in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-18-2009, 06:48 AM
  2. move files to another folder delete original
    By D_Rennie in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-09-2009, 02:50 AM
  3. Delete .csv files in a folder
    By John in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-02-2006, 02:10 PM
  4. Delete files within a folder
    By tarzan538 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-16-2006, 09:25 PM
  5. [SOLVED] Delete All Files in a folder
    By Michael Smith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-08-2005, 01:05 PM

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