Macro to exit sub if sheet not found

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have written code to email a sheet, but if the sheet does not exist, then Message box to advise "Sheet does not exist as no data available" and then to exit sub, but if sheet exits then to continue


It would be apprciated if someone could kindly amend my code

Code:
Sub Email_BR1()

Dim File As String, strBody As String, ws As Worksheet
On Error Resume Next
Set ws = Sheets("MySheet")



Application.ScreenUpdating = False
Application.DisplayAlerts = False
If Err.Number <> 0 Then
MsgBox "There are no overdue amounts for this branch, hence there is no sheet "
Else
File = Environ$("temp") & "\" & Format(Range("Q2"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"
strBody = "Hi " & Sheets("BR1").Range("U1") & vbNewLine & vbNewLine & _
"Attached, please find overdue amounts." & vbNewLine & vbNewLine & _
"The total overdue balance is " & Format(Sheets("BR1").Range("P1"), "R#,##0.00") & "." & vbNewLine & vbNewLine & _
"Regards" & vbNewLine & vbNewLine & _
"Howard"
ActiveWorkbook.Save

Sheets("BR1").Copy
With ActiveWorkbook
.SaveAs Filename:=File, FileFormat:=51
.Close savechanges:=False
End With
With CreateObject("Outlook.Application").CreateItem(0)
.Display
.to = Join(Application.Transpose(Sheets("BR1").Range("V1:V2").Value), ";")
.Subject = "overdue Debtors"
.body = strBody
.Attachments.Add File
'.Send
End With
End If
Kill File
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub [code]
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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