Hey there,
I am trying to write code in the VB behind Excel that will allow me to navigate to an excel file (can be open or closed) and delete it.
ie navigates to file xxx.xls at N:/xxx/xxx/ selects the file and then deletes it.
Any ideas?
Hey there,
I am trying to write code in the VB behind Excel that will allow me to navigate to an excel file (can be open or closed) and delete it.
ie navigates to file xxx.xls at N:/xxx/xxx/ selects the file and then deletes it.
Any ideas?
Re: Navigate To And Delete Workbook Vb Code
If you had done a search for "delete workbook", you'd already have your answer. You can also search for "Kill Statement" in visual basic help.
Re: Navigate To And Delete Workbook Vb Code
Sub KillWb()
Dim wb As Workbook
Dim strBookPathName As String
'Close workbook if open
On Error Resume Next
Set wb = Workbooks("Book1.xls")
On Error GoTo 0
If Not wb Is Nothing Then
strBookPathName = wb.FullName
wb.Close False
Else
strBookPathName = "Your file path and name"
End If
Kill strBookPathName
End Sub
Display More
Re: Delete Open or Closed Workbook Code
Thanks for the help Oz and Dave Hawley.
Much appreciated[hr]*[/hr] Auto Merged Post;[dl]*[/dl]And thanks to shg, i will use that code also[hr]*[/hr] Auto Merged Post;[dl]*[/dl]Solution worked out as:
Thank you to all who helped
Re: Navigate To And Delete Workbook Vb Code
Quote from Dave Hawley;380843Code Display MoreSub KillWb() Dim wb As Workbook Dim strBookPathName As String 'Close workbook if open On Error Resume Next Set wb = Workbooks("Book1.xls") On Error GoTo 0 If Not wb Is Nothing Then strBookPathName = wb.FullName wb.Close False Else strBookPathName = "Your file path and name" End If Kill strBookPathName End Sub
Through the magic of the internet I came across this code which looks like it would work perfectly, but doesn't. Since this I"m now using Excel 2016 something must have changed in the VBA coding language. Here's the error message I'm getting:
I still have the workbook open. The code neither closes the workbook, nor deletes the file, but the error states 'File not found'. Any ideas?
Thanks in advance!
Cheers!
Re: Delete Open or Closed Workbook Code
Hi TrainingGoddess, welcome to OzGrid.
Please can you start a new thread for your question and provide a link back to this thread if you feel it will help understand. Asking your own question in another person's thread is something we don't do here
Kind regards,
S O
Don’t have an account yet? Register yourself now and be a part of our community!