我正在尝试将三张工作表(两张是数据透视表,一张是这些数据透视表的源数据)从一个工作簿复制到一个新工作簿。
下面的代码将所需的工作表复制到新工作簿并保存(修改后的原始工作表):
Sub ExportFile()
Dim NewName As String
Dim nm As Name
Dim ws As Worksheet
With Application
.ScreenUpdating = False
On Error GoTo ErrCatcher
' Array of sheets to copy
Sheets(Array("sourcedata", "pivot", "pivot2")).Copy
On Error GoTo 0
For Each ws In ActiveWorkbook.Worksheets
ws.Cells.Copy
' Paste sheets
ws.[A1].PasteSpecial
' Remove external links, hyperlinks and hard-code formulas
ws.Cells.Hyperlinks.Delete
Application.CutCopyMode = False
' Select A1 on sheet
Cells(1, 1).Select
ws.Activate
Next ws
Cells(1, 1).Select
' Save it in the same directory as original and close
ActiveWorkbook.SaveCopyAs ThisWorkbook.Path & "\export.xls"
ActiveWorkbook.Close SaveChanges:=False
.ScreenUpdating = True
End With
Exit Sub
ErrCatcher:
MsgBox "Specified sheets do not exist within this workbook"
End Sub
但是,当我打开新工作簿时,数据透视表中的数据源仍指向原始工作簿。我的同事解释说,这是因为表格是一张一张地复制的,而不是一组复制的,并建议将表格复制为一个范围。我将如何将工作表复制为一个范围,或者将数据源更改为新复制的工作表是否更简单?