-
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
-
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]
-
<?>
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.