我在这个帖子中找到了解决方案的基本部分 Copying range and pasting into new workbook
然而,当我试图给它我的范围
wsI.Range("A1:Q1500").Copy
我收到“运行时错误1004”。
我是否需要使用其他一些复制范围的方法?
为了便于使用,这里是我链接的帖子中的完整略微编辑的代码
Option Explicit
Sub Sample()
Dim wbI As Workbook, wbO As Workbook
Dim wsI As Worksheet, wsO As Worksheet
'~~> Source/Input Workbook
Set wbI = ThisWorkbook
'~~> Set the relevant sheet from where you want to copy
Set wsI = wbI.Sheets("SICAV")
'~~> Destination/Output Workbook
Set wbO = Workbooks.Add
With wbO
'~~> Set the relevant sheet to where you want to paste
Set wsO = wbO.Sheets("Sheet1")
'~~>. Save the file
.SaveAs Filename:="Pictay.xls", FileFormat:=56
'~~> Copy the range
wsI.Range("A1:Q10").Copy
'~~> Paste it in say Cell A1. Change as applicable
wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
End With
End Sub
答案 0 :(得分:1)
刚看到你的问题并运行了代码。它工作正常。 首先,您必须对导致错误的原因感到好奇。一些research对此有所帮助。
非常简单的答案是,当您为工作簿指定一个已定义的名称,然后在不先保存和/或关闭工作簿的情况下多次复制工作表时,可能会出现类似这样的问题。
例如: -
For i = 1 To 10
wsI.Range("A1:Q1500").Copy
wsO.Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Next i
Microsoft建议您在复制过程中定期保存并关闭工作簿,如以下示例代码所示:
For i = 1 To 10
wsI.Worksheets(i).Copy After:=wsO.Worksheets(i)
'Save, close, and reopen after every 100 iterations:
If i Mod 100 = 0 Then
wsO.Close SaveChanges:=True
Set wsO = Nothing
'' please change the path to your workbook below.
Set wsO = Application.Workbooks.Open("c:\wsO.xls")
End If
Next