Email to attach file but to exit sub if Rage G1 = 0

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below which attaches the specified sheet

However where G1 on the specified sheet is 0, then the macro to exit, otherwise attach sheet

when running the macro an where G1 is zero , the sheet is being generated, which it must not be generated

It would be if someone could kindly check my Macro and amend

Code:
 Sub Email_Report()[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Dim File As String, strBody As String[/TD]
[/TR]
[TR]
[TD]Application.ScreenUpdating = False[/TD]
[/TR]
[TR]
[TD]Application.DisplayAlerts = False[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]File = Environ$("temp") & "\" & Format(Range("Q2"), "mmm-yy ") & Format(Now, "dd-mmm-yy h-mm-ss") & ".xlsx"[/TD]
[/TR]
[TR]
[TD]strBody = "Hi " & Sheets("BR1").Range("S2") & vbNewLine & vbNewLine & _[/TD]
[/TR]
[TR]
[TD]"Attached, please find " & Sheets("BR1").Range("A1") & vbNewLine & vbNewLine & _[/TD]
[/TR]
[TR]
[TD]"Please email me your sales report by department" & vbNewLine & vbNewLine & _[/TD]
[/TR]
[TR]
[TD]"Regards" & vbNewLine & vbNewLine & _[/TD]
[/TR]
[TR]
[TD]"Howard"[/TD]
[/TR]
[TR]
[TD]ActiveWorkbook.Save[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Sheets("BR1").Copy[/TD]
[/TR]
[TR]
[TD]With ActiveWorkbook[/TD]
[/TR]
[TR]
[TD]If Range("g1") = 0 Then[/TD]
[/TR]
[TR]
[TD]Exit Sub[/TD]
[/TR]
[TR]
[TD]ActiveWorkbook.Close[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Else[/TD]
[/TR]
[TR]
[TD]End If[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD].SaveAs Filename:=File, FileFormat:=51[/TD]
[/TR]
[TR]
[TD].Close savechanges:=False[/TD]
[/TR]
[TR]
[TD]End With[/TD]
[/TR]
[TR]
[TD]With CreateObject("Outlook.Application").CreateItem(0)[/TD]
[/TR]
[TR]
[TD].Display[/TD]
[/TR]
[TR]
[TD].to = Join(Application.Transpose(Sheets("BR1").Range("R2:R5").Value), ";")[/TD]
[/TR]
[TR]
[TD].Subject = Sheets("BR1").Range("A1")[/TD]
[/TR]
[TR]
[TD].body = strBody[/TD]
[/TR]
[TR]
[TD].Attachments.Add File[/TD]
[/TR]
[TR]
[TD]'.Send[/TD]
[/TR]
[TR]
[TD]End With[/TD]
[/TR]
[TR]
[TD]Kill File[/TD]
[/TR]
[TR]
[TD]Application.ScreenUpdating = True[/TD]
[/TR]
[TR]
[TD]Application.DisplayAlerts = True[/TD]
[/TR]
[TR]
[TD]End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Problem resolved

Code:
 With Sheets("BR1")
    If .Range("G1") = 0 Then
    Exit Sub
     Else
     End If
       end With
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,806
Members
449,048
Latest member
greyangel23

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