Create a copy of a existing worksheet / Saving the sheet as values only (as new file) / then emailing out

DataMonkeyNo1

New Member
Joined
Sep 12, 2013
Messages
11
Hi Fellow Excelians,

This is my first post on here so please be patient. I am currently trying to automate a report, I know the refresh and email out subs easily enough, but I've attempted to include a section to save over as new values, at which point I get

[h=1]"Run Time error '9' - Subscript out of range"[/h]
Below is the VBA I have been using (I have omitted some areas to XXX):

/VBA for copy over a worksheet to a new file (as values) THEN save the file to a specified location THEN to E-mail the file out to a specified group of people.


Private Sub Workbook_Open()


ActiveWorkbook.RefreshAll


Application.DisplayAlerts = False
Worksheets("Rpt").Copy
With ActiveSheet.UsedRange
.Value = .Value
End With
Set wbNew = ActiveWorkbook
wbNew.SaveAs "XXX\Daily Stock Bible Sent Today.xls"
wbNew.Close True
Application.DisplayAlerts = True


Dim OutApp As Object
Dim OutMail As Object


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)


On Error Resume Next
With OutMail
.to = "XXX"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "It Bloody Works"
.Attachments.Add ActiveWorkbook.FullName
.send
End With
On Error GoTo 0


Set OutMail = Nothing
Set OutApp = Nothing

End Sub


Utmost thanks for anyone who spends any time in assisting me with this.
 
Which line is causing the error? If it's this one:

Worksheets("Rpt").Copy

make sure that you have a worksheet named Rpt in the workbook that contains your VBA code.
 
Upvote 0
Please try:

Code:
Private Sub WorkbookCopyAndSend()

    Dim Source, Target As Workbook
    Dim ReportS, ReportT As Worksheet

    Set Source = ActiveWorkbook
    Set Target = Workbooks.Add
    Set ReportS = Target.Sheets("Rpt")

    Source.RefreshAll

    With Application
        .DisplayAlerts = False
        .ScreenUpdating = False
    End With

    ReportS.Copy Before:=Target.Sheets(1)

    With Target
        .Sheets(Array("Sheet1", "Sheet2", "Sheet3")).Delete
        Set ReportT = .Sheets("Rpt")
        With ReportT.UsedRange
            .Copy
            .PasteSpecial xlPasteValues
        End With
        .SaveAs "XXX\Daily Stock Bible Sent Today.xls"
        .Close
    End With

    With Application
        .DisplayAlerts = True
        .ScreenUpdating = True
    End With

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


    On Error Resume Next
    With OutMail
        .To = "XXX"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = "It Bloody Works"
        .Attachments.Add Target.FullName
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

End Sub

Let us know if this works.

Thanks,

J.
 
Upvote 0

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