VBA to loop through sheets and copy to new workbook including the pivot tab

simont1975

New Member
Joined
Nov 5, 2015
Messages
8
Hi,

Apologies if this has been asked before, I wasn't able to find anything similar.

I have code that loops through all worksheets except certain sheets and will save a copy of the sheet to a new workbook. (code below). What I'm trying to do is do exactly the same but also attach the pivot tab to the new workbook and ensure the pivot cache is only looking at the new data.

My currently VBA (Which works, but id like to also copy the pivot tab to each newly save workbook based on the looping of the worksheets except those identified)

'Save Sheets for Handlers into Folder
Sub SaveShtsAsBook_NEW()
Dim Sheet As Worksheet, SheetName$, MyFilePath$, N&
MyFilePath$ = ActiveWorkbook.Path & "\" & _
Left(Range("A1").Value, Len(Range("A1").Value))
'Above is the name of the folder created to store the Debtor Files

With Application
.ScreenUpdating = False
.DisplayAlerts = False
' End With
On Error Resume Next '<< a folder exists
MkDir MyFilePath '<< create a folder


For Each Sheet In Worksheets
If Sheet.Name <> "Refresh" And Sheet.Name <> "PIVOT" And Sheet.Name <> "IBA CL064 - DATA" And Sheet.Name <> "AGE_MAP" And Sheet.Name <> "BU Mapping" And Sheet.Name <> "BU Listing" And Sheet.Name <> "IBA_Acct_Hand" And Sheet.Name <> "OPS_Mapping" And Sheet.Name <> "FO_Acct_Hand_by_Pol" And Sheet.Name <> "TeamName_Mapping" Then
Sheet.Copy
With ActiveWorkbook
With .ActiveSheet
[A1].Select
SheetName = ActiveSheet.Name
End With
.SaveAs Filename:=MyFilePath _
& "\" & SheetName & ".xlsx"
.Close SaveChanges:=True
End With
.CutCopyMode = False
End If
Next
End With

Worksheets("IBA CL064 - DATA").Activate
Range("AR:AR").Clear

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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