programsam
Board Regular
- Joined
- Feb 10, 2016
- Messages
- 123
I have a pivot table created in Power Pivot, it's on a sheet ("Sups") that when using VBA to copy and paste will work sometimes and in other instances, it will not, triggering the "Copy method of worksheet class failed" error.
The line that triggers the error is below. Any thoughts?
The line that triggers the error is below. Any thoughts?
VBA Code:
Sub sendEmail()
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim OutMail As Outlook.MailItem
Set OutMail = outlookApp.CreateItem(olMailItem)
Dim toList As String, ccList As String, filepath As String
Dim year As String
Dim today As Date, yesterday As Date
Dim dailyWb As Workbook
Dim openWb As Workbook
Set dailyWb = ThisWorkbook
Dim range_data As Range, range_data2 As Range, range_data3 As Range, range_data4 As Range, range_data5 As Range
Dim path As String, stats As String
year = dailyWb.Sheets("Values").Range("K5").Value
today = dailyWb.Sheets("Values").Range("K1").Value
yesterday = dailyWb.Sheets("Values").Range("K2").Value
filepath = dailyWb.Sheets("Values").Range("K6").Value
toList = dailyWb.Sheets("Values").Range("K13").Value
ccList = dailyWb.Sheets("Values").Range("K14").Value
stats = dailyWb.Sheets("Values").Range("C22").Value
Call refresher
Set newWb = Workbooks.Add
'Copying a worksheet from ThisWorkbook into newly created workbook in the above statement
dailyWb.Worksheets("Reps").Visible = True
dailyWb.Sheets("Reps").Copy Before:=newWb.Sheets(1)
newWb.Sheets(1).UsedRange.Value = ActiveSheet.UsedRange.Value
newWb.Sheets(1).Columns("A:K").Delete
newWb.Sheets(1).Range("C3").Select
ActiveWindow.FreezePanes = True
dailyWb.Worksheets("Booked Orders").Visible = True
dailyWb.Sheets("Booked Orders").Copy After:=newWb.Sheets("Reps")
'newWb.Sheets("New Orders").Columns("O").Delete
dailyWb.Worksheets("New Orders").Visible = True
dailyWb.Sheets("New Orders").Copy After:=newWb.Sheets("Booked Orders")
newWb.Sheets("New Orders").Columns("O").Delete
'newWB.Sheets("New Orders").UsedRange.Value = ActiveSheet.UsedRange.Value
dailyWb.Worksheets("Shipped Sales").Visible = True
dailyWb.Sheets("Shipped Sales").Copy Before:=newWb.Sheets("New Orders")
'newWB.Sheets("New Orders").UsedRange.Value = ActiveSheet.UsedRange.Value
dailyWb.Worksheets("Rep Bonus").Visible = True
dailyWb.Sheets("Rep Bonus").Copy After:=newWb.Sheets("New Orders")
'newWb.Sheets("Rep Bonus").UsedRange.Value = ActiveSheet.UsedRange.Value
dailyWb.Worksheets("Dispositions MTD").Visible = True
dailyWb.Sheets("Dispositions MTD").Copy Before:=newWb.Sheets("Rep Bonus")
dailyWb.Worksheets("Interaction").Visible = True
dailyWb.Sheets("Interaction").Copy Before:=newWb.Sheets("Reps Bonus")
dailyWb.Worksheets("Sups").Visible = True
[B] dailyWb.Sheets("Sups").Copy Before:=newWb.Sheets("Reps Bonus")[/B]
newWb.Sheets("Reps").Activate
End Sub