Macro to Email several sheets, but not to email those where A2 is blank

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,566
Office Version
  1. 2021
Platform
  1. Windows
I have macro to email several sheets, but do not want to attach those sheets where A2 is blank


It would be appreciated if someone could amend my code




Code:
 Sub Email_salesheets()[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Dim Ztext As String[/TD]
[/TR]
[TR]
[TD]Dim Zsubject As String[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Ztext = [bodytext] 'read in text from named cell[/TD]
[/TR]
[TR]
[TD]Zsubject = [SubjectText][/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]Sheets(Array("sales1", "sales2", "sales3")).Copy[/TD]
[/TR]
[TR]
[TD][/TD]
[/TR]
[TR]
[TD]With Range("A1:O150")[/TD]
[/TR]
[TR]
[TD].Value = .Value[/TD]
[/TR]
[TR]
[TD]End With
With ActiveWorkbook
'.Windows(1).Visible = False
Application.DisplayAlerts = False
.SaveAs Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"
Application.DisplayAlerts = True
.Close (True)
End With
With CreateObject("Outlook.Application").CreateItem(0)
.To = Join(Application.Transpose(Sheets("Email sales").Range("AA1:AA3").Value), ";")
.Subject = Zsubject
.Body = Ztext
.Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"
.Display
'.send
End With
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Sorry but the way you have pasted in your code makes it unreadable. My recommendation is to edit your post, or add a new post. Go to your VBA and Copy it, then go to your post and Paste it. Then highlight all the code in your post and click the VBA button in the edit controls.
 
Upvote 0
Hi Jeff

My apologies. There is no edit button, So am going to create a new post


 
Upvote 0
Hi Jeff


See code below

Code:
 Sub Email_salesheets()



Dim Ztext As String

Dim Zsubject As String







Ztext = [bodytext] 'read in text from named cell

Zsubject = [SubjectText]



Sheets(Array("sales1", "sales2", "sales3")).Copy



With Range("A1:O150")

.Value = .Value

End With



With ActiveWorkbook

'.Windows(1).Visible = False

Application.DisplayAlerts = False

.SaveAs Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"



Application.DisplayAlerts = True

.Close (True)

End With



With CreateObject("Outlook.Application").CreateItem(0)

.To = Join(Application.Transpose(Sheets("Email sales").Range("AA1:AA3").Value), ";")

.Subject = Zsubject

.Body = Ztext



.Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"

.Display

'.send

End With



End Sub
 
Upvote 0
I only see you attaching one file here. I also don't understand what the intent is of your code. For example,

VBA Code:
.SaveAs Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"
This code saves the activeworkbook. It seems to be saving it in a temp folder just for the purpose of being able to attach it later. Also sheets("sales1").Name is just "sales1". It's kind of a pointless operation. This code will save a file called sales1.xlsx.

VBA Code:
.Attachments.Add Environ("TMP") & "\" & ThisWorkbook.Sheets("sales1").Name & ".xlsx"
As above, this code is attaching a file from your TMP folder called sales1.xlsx.

You are just attaching a copy of the active workbook. Why not just attach the active workbook itself and skip all of this saving?
VBA Code:
.Attachments.Add ActiveWorkbook.FullName

None of this answer your question but I'm still unclear as to the question. Do you want to attach a version of the active workbook that removes any sheets where A2 is blank?
 
Upvote 0

Forum statistics

Threads
1,215,417
Messages
6,124,783
Members
449,188
Latest member
Hoffk036

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