Copy worksheets to new workbooks incl. Pivot Table

OBMIKEE

New Member
Joined
May 24, 2017
Messages
8
I have a workbook that contains multiple worksheets
I wish to copy each ws to a new wb and save each seperately.

This works well with below, until i introduce a pivot table into the worksheets.
1004 - you cannot nove part of a PivotTable or insert worksheet cells rows..........
Code:
Sub Generate_Files()
'
' Generate_files
'


'
  Dim sheetnames As Variant
  
  sheetnames = Array("STRATEGY", "MARKETING", "GROUP", "INNOVATION")


  Dim i As Long
  For i = 0 To 36
    Windows("Dev_workbook.xlsm").Activate
    Sheets(sheetnames(i)).Select
    Sheets(sheetnames(i)).Copy
    ActiveSheet.UsedRange.Value = ActiveSheet.UsedRange.Value
    Path = "C:\Users\xyz\Desktop\Mike"
    Filename = sheetnames(i) & Range("G1") & Range("F1")
   ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
   ActiveWorkbook.Close
    
    Next
End Sub


Any assistance appreciated
 
Last edited by a moderator:
Not too surprising then - you removed the line that copies the worksheet! ;)

Also, please use code tags when posting code.
 
Last edited:
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Same behaviour changing those two lines to
Sub Generate_Files()
'
' Generate_files
'


'
Dim sheetnames As Variant

sheetnames = Array("STRATEGY", "MARKETING", "GROUP", "INNOVATION")


Dim i As Long
For i = 0 To 36
Windows("Dev_workbook.xlsm").Activate
Sheets(sheetnames(i)).Select
ActiveSheet.UsedRange.Copy
ActiveSheet.UsedRange.PasteSpecial xlPasteValues
Path = "C:\Users\obriemik\Desktop\Mike\Mike1"
Filename = sheetnames(i) & Range("G1") & Range("F1")
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWorkbook.Close

Next
End Sub


Tried both to see if it gets back into loop
 
Upvote 0
Thanks @RoryA

Sometimes you can look at the same code for far too long and not see the wood for the trees :)
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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