Excel宏:粘贴时出错 - 向下移动10行 - 粘贴 - 10行 - 粘贴等

时间:2010-08-25 14:21:50

标签: excel vba excel-vba

我正在尝试从同一范围(A4:A8)从三个工作簿(一次一个)复制数据并将其粘贴到新工作簿中。唯一的问题是我试图让每个循环中的粘贴部分向下移动10行。

我在PasteSpecial行上收到错误消息。我不确定为什么?

(我是一名Excel宏新手,但我还是试着去了)

非常感谢任何指导/指示。

非常感谢

麦克

Dim TestWorkbook as variant,
Dim mytest as variant
TestWorkbook= array(“test1”, “test2”, “test3”) ‘3 source files
i = 0
 For Each mytest  In TestWorkbook 
 Workbooks.Open Filename:="S:\ExcelWork\" + TestWorkbook + "_Work.xls"
 Range("a4:a8").Copy
 ActiveWorkbook.Close
 Workbooks.Open Filename:="S:\Result_Workbook.xls"
 Range("l" & 5 + i).PasteSpecial Paste:=xlPasteValues `THE PROBLEM IS HERE
 i = i + 10  
Next

1 个答案:

答案 0 :(得分:2)

修复了几个问题'见评论

Sub a()
 Dim TestWorkbook As Variant
 Dim mytest As Variant
 Dim s As String
 TestWorkbook = Array("test1", "test2", "test3")
 i = 1  'Start with 1
 Workbooks.Open Filename:="S:\Result_Workbook.xls" 'open only once
 For Each mytest In TestWorkbook
     s = mytest & ".xls"
     Workbooks.Open "c:\" & s
     Workbooks(s).Activate 'Remember to Activate
     Range("a4:a8").Copy
     ' Now paste properly
     Workbooks("Result_Workbook.xls").Worksheets("Sheet1").Range("l" & 5 + i).PasteSpecial Paste:=xlPasteValues
     ActiveWorkbook.Close SaveChanges:=False 'close after pasting
     i = i + 10
 Next
 End Sub