VBA: Delete sheet then mail workbook

Groovey

New Member
Joined
Aug 27, 2015
Messages
3
Hi guys!
:warning:ATTENTION NEWBIE IN ACTION:warning:
So i recently started working with excel and VBA at work, and got hooked right away! However, now i'm standing in neck deep in spreadsheet(... get it?) and need some fresh eyes to point out my error. The goal is to have a macro that once executed, will delete a single sheet, then e-mail the workbook as an attachment. However, at the moment, it only deletes the sheet from the original workbook, and not the attachment...

(Credit to Excel Automation - Ron de Bruin for showing me the way of the spreadsheet)

Sub MyBrokenSub()

Dim MailApp As Object
Dim MailCreate As Object
Dim WB1 As Workbook

Set MailApp = CreateObject("Outlook.Application")
Set MailCreate = OutApp.CreateItem(0)
Set WB1 = ActiveWorkbook

With WB1
Sheets("MySheet").Delete
End With

On Error Resume Next
With OutMail
.to = ”something@something.totallyalegitdomain"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add WB1.FullName
.Send
End With
On Error GoTo 0

Set MailApp = Nothing
Set MailCreate = Nothing
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think the ActiveWorkbook reference isn't targeting the book you are intending.

Code:
[FONT=Arial Narrow]Set MailApp = CreateObject("Outlook.Application")
    Set MailCreate = OutApp.CreateItem(0)
    Set WB1 = ActiveWorkbook

    With WB1
        Sheets("MySheet").Delete
    End With[/FONT]

Try: Set WB1 = Workbooks(filename)

Then in the With WB1 section, put a dot before your Sheets("MySheet").Delete or replace entire with statement with:
WB1.Sheets("MySheet").Delete
 
Upvote 0
I think the ActiveWorkbook reference isn't targeting the book you are intending.



Try: Set WB1 = Workbooks(filename)

Then in the With WB1 section, put a dot before your Sheets("MySheet").Delete or replace entire with statement with:
WB1.Sheets("MySheet").Delete

Unfortunately this did not work. Im thinking about:

MySheet.Cells.Clear


and then

Sheets("MySheet").Visible = xlVeryHidden

Thoughts?
 
Upvote 0
Don't have Excel at the moment, so UNTESTED....but try
Code:
Sub MyBrokenSub()

Dim MailApp As Object, outmail As Object, thisWb As Workbook

Set MailApp = CreateObject("Outlook.Application")
Set outmail = OutApp.CreateItem(0)
Set thisWb = ActiveWorkbook
ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\new workbook.xls"
Set tempWB = ActiveWorkbook
Application.DisplayAlerts = False
    tempWB.Sheets("MySheet").Delete
Application.DisplayAlerts = True
On Error Resume Next
With outmail
.to = "" '”something@something.totallyalegitdomain"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add tempWB.FullName
.Send
End With
On Error GoTo 0

Set MailApp = Nothing
Set MailCreate = Nothing
End Sub
 
Upvote 0
Don't have Excel at the moment, so UNTESTED....but try
Code:
Sub MyBrokenSub()

Dim MailApp As Object, outmail As Object, thisWb As Workbook

Set MailApp = CreateObject("Outlook.Application")
Set outmail = OutApp.CreateItem(0)
Set thisWb = ActiveWorkbook
ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\new workbook.xls"
Set tempWB = ActiveWorkbook
Application.DisplayAlerts = False
    tempWB.Sheets("MySheet").Delete
Application.DisplayAlerts = True
On Error Resume Next
With outmail
.to = "" '”something@something.totallyalegitdomain"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add tempWB.FullName
.Send
End With
On Error GoTo 0

Set MailApp = Nothing
Set MailCreate = Nothing
End Sub

I fixed it... Kicking myself for not thinking of it...
With WB1
Application.DisplayAlerts = False
WB1.Sheets("MySheet").Delete
WB1.Save
Application.DisplayAlerts = True
End With
Thank you all for the input. Im impressed with how fast people stand ready to help you. :)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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