[英]Error 1004 - Opening and activating workbooks
I have some issues while trying to copy/paste data between workbooks. 尝试在工作簿之间复制/粘贴数据时遇到一些问题。 I need to extract data from two different workbooks (A & B) to put it in a third one.
我需要从两个不同的工作簿(A和B)中提取数据,然后将其放入第三个工作簿中。 Since A & B have the exact same structure, I use the same code for both of them.
由于A和B具有完全相同的结构,因此我对两者使用相同的代码。 However it works for A and I've got an error 1004 for B.
但是,它适用于A,并且B出现错误1004。
It seems that it happens when you do not specify the parent workbook/worksheet properly but I don't think this is the issue here since the code works for A. 如果您没有正确指定父级工作簿/工作表,但我认为这不是问题,因为代码适用于A。
If someone has an insight on this matter, I'm all ears! 如果有人对此事有见识,我会很高兴!
Thank you for your help! 谢谢您的帮助!
CH CH
Sub Data_Extraction()
Dim wb As Workbook, wba As Workbook, wbb As Workbook
Set wb = ActiveWorkbook
Set wba= Workbooks.Open("D:\xxx\A.xlsx")
Set wbb= Workbooks.Open("D:\xxx\B.xlsx")
Dim wsa As Worksheet, wsb As Worksheet
Set wsa = wb.Worksheets("a")
Set wsb = wb.Worksheets("b")
'I use a named variable here
X = Range("X")
If X=2 Then
''We fill the tab a''
For i = 9 To 400
wba.Activate
If wba.Worksheets("a").Cells(i, 2).Value = 5 Then
wba.Worksheets("a").Range(Cells(i, 1), Cells(i, 8)).Copy
wb.Activate
wsa.Range(Cells(7, 2), Cells(7, 9)).PasteSpecial Paste:=xlPasteValues
wsa.Range("B7").EntireRow.Insert
End If
Next i
''We fill the tab b''
For i = 9 To 400
wbb.Activate
If wbb.Worksheets("b").Cells(i, 2).Value = 5 Then
wbb.Worksheets("b").Range(Cells(i, 1), Cells(i, 8)).Copy
wb.Activate
wsb.Range(Cells(7, 2), Cells(7, 9)).PasteSpecial Paste:=xlPasteValues
wsb.Range("B7").EntireRow.Insert
End If
Next i
End If
End Sub
In lots of cases when you use Excel methods like protect/unprotect copy/paste, you should try to mimick as close as possible the configuration Excel would be in when a user would go through those steps, if you step away from that you are likely to wind up with instability cropping up in generic error codes as 5 and 1004. 在许多情况下,当您使用Excel方法(如保护/取消保护复制/粘贴)时,应尽量模仿用户在执行这些步骤时Excel所处的配置,如果您远离这些配置,则很可能最终出现不稳定,出现在通用错误代码5和1004中。
In this case I believe you should do 在这种情况下,我相信你应该做
wbb.Worksheets("b").Activate
before you start a copy from worksheet("b"). 从工作表(“ b”)开始复制之前 。
The real problem here is not that you didn't activate the sheet (though that does work, it's not a good solution) 真正的问题不是您没有激活工作表(尽管可以工作,但这不是一个好的解决方案)
wbb.Worksheets("b").Range(Cells(i, 1), Cells(i, 8)).Copy
Here the Cells()
calls (unlike the Range()
call) are not qualified by any worksheet object, so they will default to the ActiveSheet. 这里的
Cells()
调用(不同于Range()
调用)不受任何工作表对象的限制,因此它们将默认为ActiveSheet。 In a regular module this is equivalent to writing: 在常规模块中,这等效于编写:
wbb.Worksheets("b").Range(ActiveSheet.Cells(i, 1), ActiveSheet.Cells(i, 8)).Copy
...and is prone to failure when the active sheet is not what you expect. ...如果活动工作表不是您期望的那样,则容易发生故障。
This is robust and doesn't require you to activate a specific worksheet: 这很强大,不需要您激活特定的工作表:
With wbb.Worksheets("b")
.Range(.Cells(i, 1), .Cells(i, 8)).Copy
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.