wiggins2402
New Member
- Joined
- Aug 5, 2016
- Messages
- 9
- Office Version
- 2016
- Platform
- Windows
I have created some VBA code to copy multiple Pivot Tables on different tabs and Paste them in a new workbook in a temp file location to be emailed from a list in the source workbook. Everything works well except when I paste the pivot tables it only paste the data and not the format. I have searched multiple web blogs as well as throughout Mr. Excel and could not find a solution that would work. Below is the code I am using and I am probably missing something simple in it. If you know a solution U would be very grateful.
Code:
Sub ExportPivotTables()
Dim terr As String
Dim TempFilePath As String
Dim TempFileName As String
Dim FileExtStr As String
Dim OutApp As Object
Dim OutMail As Object
Dim MyMonth As String
Dim MyYear As String
Dim SubjectLine As String
Dim FileFormatNum As Long
Dim row As Integer
' Email Subject line.
MyMonth = Format(DateAdd("m", -1, Date), "mmmm")
MyYear = CStr(Format(DateAdd("m", -1, Date), "yyyy"))
SubjectLine = MyMonth + " " + MyYear + " Weidmuller Region Sales Report "
FileFormatNum = 51
FileExtStr = ".xlsx"
row = 2
On Error Resume Next
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Worksheets("Regions").Activate
ActiveSheet.Cells(row, 1).Select
Do Until ActiveCell.Value = "End"
terr = ActiveCell.Value
ActiveSheet.Cells(row, 2).Select
Email = ActiveCell.Value
Sheets("Region Overview").Select
ActiveSheet.PivotTables ("Region Overview")
ActiveSheet.PivotTables("Region Overview").PivotFields("Region").ClearAllFilters
ActiveSheet.PivotTables("Region Overview").PivotFields("Region").CurrentPage = terr
TempFilePath = Environ$("temp") & "\"
TempFileName = "Sales Report (" + terr + ")"
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Call CopyPivotValues(TempFilePath & TempFileName & FileExtStr, ActiveWorkbook)
On Error Resume Next
With OutMail
.To = Email
.CC = ""
.BCC = ""
.Subject = SubjectLine
.Body = "Sales Team, Attached is your Sales report for the previous month for your territory."
.Attachments.Add TempFilePath & TempFileName & FileExtStr
.send
End With
On Error GoTo 0
Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
Worksheets("Regions").Activate
row = row + 1
ActiveSheet.Cells(row, 1).Select
Loop
End Sub
Sub CopyPivotValues(fname As String, sWB As Workbook)
Dim dWB As Workbook
Dim MySheets, i As Long, ws As Worksheet, Rng As Range
MySheets = Array("Region Overview", "Region Totals", "Region Totals by Customer Type", "Territory Totals", "Territory Totals by Cus Type", "DIR(POP) Sales", "DIS(POP) Sales", "POS Disty Summary", "POS Disty POS Sales w-Customer", "POP Disty POS Sales w-Customer", "Samples")
Set dWB = Workbooks.Add
Const NewwbName As String = "Territory Sales Report"
With Application
.ScreenUpdating = 0
.DisplayAlerts = 0
End With
For i = 0 To UBound(MySheets)
On Error Resume Next
Set ws = dWB.Sheets(MySheets(i))
On Error GoTo 0
With sWB.Sheets(MySheets(i))
Set Rng = .Range("a1", .Range("a1").SpecialCells(xlCellTypeLastCell))
Rng.UnMerge
End With
With dWB
If ws Is Nothing Then
Set ws = Sheets.Add
ws.Move After:=Sheets(dWB.Sheets.Count)
ws.Name = MySheets(i)
End If
Rng.Copy
ws.Range("a1").PasteSpecial xlFormats
ws.Range("a1").PasteSpecial xlValues
ws.Range("a1").PasteSpecial xlPasteColumnWidths
Set ws = Nothing
Set Rng = Nothing
End With
Next
dWB.Sheets(1).Delete
dWB.SaveAs Filename:=fname
dWB.Close False
With Application
.ScreenUpdating = 1
.DisplayAlerts = 1
End With
End Sub