在循环中创build工作表的副本

我正在尝试复制选定的工作表,将每个工作表另存为一个不同的文件。 它不断给我一个error 1004Copy method of Worksheet class failed 。 selectSheets(i).Copy行。 我需要它来做表格副本,而不是复制和粘贴其他地方的值,因为我需要格式化结转。

任何人都可以帮助解决这个错误,因为我找不到我做错了什么?

 Sub CSR() Dim wb As Workbook, First As Integer, Last As Integer, i As Integer, j As Integer Dim rng As Range Dim wk As Worksheet Dim filepath As String Dim filename As String Set wb = ThisWorkbook filepath = "H:\Finance\Danny Bland\" wb.Activate First = Sheets("LIVE FLEET").Index Last = Sheets("Email Attachment").Index For i = First + 1 To Last - 1 wb.Activate Set wk = wb.Sheets(i) wk.Activate filename = Sheets(i).Name Sheets(i).Activate Sheets(i).Copy Cells.Copy Range("A1").PasteSpecial xlPasteValues ActiveWorkbook.SaveAs filename:=filepath & filename & ".xlsx" ActiveWorkbook.Close Next i End Sub 

尝试这个:

 Private Const filepath As String = "H:\Finance\Danny Bland\" Sub CSR() Dim wb As Workbook Dim wk As Worksheet Set wb = ThisWorkbook wb.Activate For Each wk In Sheets ' for all sheets in the workbook ' if sheet name isn't live fleet or email attachment If wk.Name <> "LIVE FLEET" Or wk.Name <> "Email Attachment" Then ExportSheet wk ' you want to export it End If Next End Sub ' you may need to modify this so exactly suit your needs Private Sub ExportSheet(sh As Worksheet) sh.Copy sh.Cells.Copy sh.Range("A1").PasteSpecial xlPasteValues sh.SaveAs filename:=filepath & sh.Name & ".xlsx" sh.Parent.Close End Sub