I am trying to create a macro that copies the values of multiple worksheets (all but the first one) from an active workbook into a new workbook for which I have put the path in cell F21 of sheet1.
Below is a code that enables me to do so for sheet2. But I can't seem to find how to adapt it so that it does it for sheets 2, 3, 4, 5, 6, 7, 8, and 9.
Another interesting thing to note is that sheet8 contains pivot tables, and it seems to be an issue when copying it to another worksheet.
Do you have any idea how I could do that ? (By the way if you have an idea how to do it, but sheet1 is included in the new file, it is not that much of a problem)
Thanks a lot.
Sub export()
Dim SourceBook As Workbook, DestBook As Workbook, SourceSheet As Worksheet, DestSheet As Worksheet
Dim SavePath As String, i As Integer
Application.ScreenUpdating = False
Set SourceBook = ThisWorkbook
SavePath = Sheets("Sheet1").Range("F21").Text
Set SourceSheet = SourceBook.Sheets("Sheet2")
Set DestBook = Workbooks.Add
Set DestSheet = DestBook.Worksheets.Add
Application.DisplayAlerts = False
For i = DestBook.Worksheets.Count To 2 Step -1
DestBook.Worksheets(i).Delete
Next i
Application.DisplayAlerts = True
SourceSheet.Cells.Copy
With DestSheet.Range("A1")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats 'Delete if you don't want formats copied
End With
DestSheet.Name = SourceSheet.Name
DestBook.Activate
With ActiveWindow
.DisplayGridlines = False
.DisplayWorkbookTabs = False
End With
SourceBook.Activate
Application.DisplayAlerts = False 'Delete if you want overwrite warning
DestBook.SaveAs Filename:=SavePath
Application.DisplayAlerts = True 'Delete if you delete other line
SavePath = DestBook.FullName
DestBook.Close 'Delete if you want to leave copy open
MsgBox ("A copy has been saved to " & SavePath)
End Sub