Results 1 to 3 of 3

Thread: How Do You Delete An Excel File From A Folder?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2000
    Location
    Minneapolis
    Posts
    12
    I am writting a macro in VBA within a Excel Workbook, and what I am having trouble doing is deleteing a Excel workbook that is in a folder called "Data" on a drive in my computer labeled "P". The Excel File is called "Temp".

    I know how to write the code to open the workbook, but I cant figure out the wording to delete it.

    Anyone??

    Douglas

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    One way to do it...

    You can use the FileSystemObject to do all kinds of neat stuff to files, folders, drives, etc. including deleting files.

    First you need to make a reference to "Microsoft Scripting Runtime", which I believe in Excel is under "Tools/References..."

    Then you can create a routine that will delete any file (assuming you have permission) such as this:
    Code:
    Private Sub DeleteFile(FilePath As String)
        Dim FileSys As New FileSystemObject
        
        FileSys.DeleteFile FilePath
        
        Set FileSys = Nothing
    End Sub
    In the above code, you need to pass the entire path for the file you want to delete to the function, i.e:

    Code:
        ' In your code somewhere...
        DeleteFile "P:\Data\Temp.xls"
    Of course if you are trying to make a file delete itself this won't work. A file must be closed for you to delete it.

    Hope that helps!

    [Edited by seaweed on 10-04-2000 at 04:19 PM]
    ~seaweed

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    If I've read this properly you want to delte a sheet or workbook form within an excel document. You know how to open so here is the code for the delete:
    Code:
    'here I delete sheet2 form the workbook
    
        Sheets("Sheet2").Select
        ActiveWindow.SelectedSheets.Delete
    If this works, here is how I got it. It may help you further on down the road. Opened excel and started a new macro and deleted the sheet and then read the macro code.

    Hope it does the trick.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width