我正在尝试复制一系列工作表,将每个工作表另存为不同的文件。它不断给我error 1004
说Copy method of Worksheet class failed
。选择Sheets(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
答案 0 :(得分:1)
试试这个:
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