How to Display Error Message if there is a macro error?

MrManBoy

New Member
Joined
May 28, 2014
Messages
37
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Hi Team,

I have the below code to delete these particular workbooks for example, however, in the case the user runs the below macro again & the file(s) have been deleted already, I would like to learn how one could stop the macro and then have a message displaying something like "Files have already been deleted" for example.

Code:
Sub DeleteWorkbooks()

Kill "E:\Steve's Stuff\Docs\Paul Test\HR.xlsx"
Kill "E:\Steve's Stuff\Docs\Paul Test\HW.xlsx"
End Sub

Thank you in advance to anyone who can help me out with this issue. :p
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
With Application
.DisplayAlerts = False
End With
before the delete routines, you don't need a warning that something has already happened before and won't happen again
 
Upvote 0
Thanks Mole999 :)

However, I am still at a lost how the above code works into resolving my original query. Are you advising that I can not have a message appear to inform the user that file(s) have already been deleted?
Please forgive me, I am still learning this thing of ours, excel, so if you wouldn't mind elaborating perhaps? :eek:

Cheers :unsure:
 
Upvote 0
Maybe try:

Code:
Sub DeleteWorkbooks()
    On Error Resume Next
    Kill "E:\Steve's Stuff\Docs\Paul Test\HR.xlsx"
    Kill "E:\Steve's Stuff\Docs\Paul Test\HW.xlsx"
    If Err <> 0 Then
        MsgBox "Files already deleted"
    End If
End Sub
 
Upvote 0
Solution
MAybe something like this
Code:
Sub DeleteWorkbooks()
Dim df1 As String, df2 As String
df1 = "E:\Steve's Stuff\Docs\Paul Test\HR.xlsx"
df2 = "E:\Steve's Stuff\Docs\Paul Test\HW.xlsx"
If Len(Dir(df1)) = 0 Or Len(Dir(df2)) = 0 Then
MsgBox "files deleted already"
Exit Sub
End If
Kill df1
Kill df2
End Sub
 
Upvote 0
Thank you Andrew and Michael for both your inputs...both codes work perfectly and is exactly what I was looking for :pray:

Cheers boys!
(y)
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,755
Members
449,049
Latest member
excelknuckles

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