Delete file on activate if exists (Should be an easy question)

Will S

Board Regular
Joined
Oct 29, 2013
Messages
69
I've got a form that creates a back up for printing (Because printing causes issues and the sheet becomes unusable) so to stop people using the printed sheet rather than the back up it created, I'd like to either delete the print sheet after printing, or to delete it when the back up is reopened.

Currently I'm trying to do it as when you open the workbook, it will delete the print copy. Issue is that when you first open the sheet, there won't be a print copy so it says cannot find file, is there a way of doing a

If file exists then
delete
else
Nothing
End If

After googling the issue I've been trying with:

Code:
Private Sub Worksheet_Activate()
Call KillFile
End Sub

Sub KillFile()
If Dir(ThisWorbook.Path & "\TransfersheetPrintCopy.xlsm") <> "" Then
Kill (ThisWorkbook.Path & "\TransfersheetPrintCopy.xlsm")
Else
End If
End Sub

That comes up with an error "Object not found" and I'm not sure why, I'm not really familiar with this Dir function so can't tell if it's the right one to use in seeing it it exists.

I'd be happy if there was a function for "Do this, if it's an error than just end" but I have no idea on the code for that.

Any help on this is greatly appreciated
Best regards
~Will S
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Code:
Sub KillFile()
 On Error GoTo ErrorHandler
 Kill (ThisWorkbook.Path & "\TransfersheetPrintCopy.xlsm")


 ErrorHandler:

End Sub
 
Upvote 0
Thank you, that works like a charm and the quick response is much appreciated.

Kind regards
~Will S
 
Upvote 0

Forum statistics

Threads
1,217,382
Messages
6,136,242
Members
450,000
Latest member
jgp19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top