Hi,
I'm currently using version 365.
I need a macro to perform the following functions in a workbook containing multiple sheets
- Copy 2 of the sheets containing pivot tables (as well as other data) to a new workbook as values
- Maintain all formatting including pivot table formatting into the new workbook and column widths
- Name the new sheets the same as the existing sheet names
- Delete 'sheet 1' in the new workbook
- Save the new workbook to the same location where existing file is located, with new workbook name specified in the code
- Open file on saving
I have modified the below which I found on an excel forum however the formatting is not copying across for the pivot tables, only for the other data. I am having trouble modifying it to copy the pivot table formatting.
Sub CopyPivotValues()
Dim sWB As Workbook
Dim dWB As Workbook
Dim MySheets, i As Long, ws As Worksheet, Rng As Range
MySheets = Array("DATA", "Rach")
Set sWB = ThisWorkbook
Set dWB = Workbooks.Add
Const NewwbName As String = "Production 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.Name = MySheets(i)
End If
Rng.Copy
ws.Range("a1").PasteSpecial xlPasteValues
ws.Range("a1").PasteSpecial xlPasteFormats
ws.Range("a1").PasteSpecial xlPasteColumnWidths
Set ws = Nothing
Set Rng = Nothing
ActiveWindow.DisplayGridlines = False
End With
Next
dWB.SaveAs Replace(sWB.FullName, sWB.Name, NewwbName & ".xlsm"), 52
dWB.Close False
With Application
.ScreenUpdating = 1
.DisplayAlerts = 1
End With
End Sub
Thank you!
I'm currently using version 365.
I need a macro to perform the following functions in a workbook containing multiple sheets
- Copy 2 of the sheets containing pivot tables (as well as other data) to a new workbook as values
- Maintain all formatting including pivot table formatting into the new workbook and column widths
- Name the new sheets the same as the existing sheet names
- Delete 'sheet 1' in the new workbook
- Save the new workbook to the same location where existing file is located, with new workbook name specified in the code
- Open file on saving
I have modified the below which I found on an excel forum however the formatting is not copying across for the pivot tables, only for the other data. I am having trouble modifying it to copy the pivot table formatting.
Sub CopyPivotValues()
Dim sWB As Workbook
Dim dWB As Workbook
Dim MySheets, i As Long, ws As Worksheet, Rng As Range
MySheets = Array("DATA", "Rach")
Set sWB = ThisWorkbook
Set dWB = Workbooks.Add
Const NewwbName As String = "Production 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.Name = MySheets(i)
End If
Rng.Copy
ws.Range("a1").PasteSpecial xlPasteValues
ws.Range("a1").PasteSpecial xlPasteFormats
ws.Range("a1").PasteSpecial xlPasteColumnWidths
Set ws = Nothing
Set Rng = Nothing
ActiveWindow.DisplayGridlines = False
End With
Next
dWB.SaveAs Replace(sWB.FullName, sWB.Name, NewwbName & ".xlsm"), 52
dWB.Close False
With Application
.ScreenUpdating = 1
.DisplayAlerts = 1
End With
End Sub
Thank you!