Vba 将附加数据复制到其他工作表
我在同一个工作簿中有以下内容,如何在不同的工作簿中使用摘要表Vba 将附加数据复制到其他工作表,vba,excel,Vba,Excel,我在同一个工作簿中有以下内容,如何在不同的工作簿中使用摘要表 Sub SummurizeSheets() Dim ws As Worksheet Application.ScreenUpdating = False Sheets("Summary").Activate For Each ws In Worksheets If ws.Name <> "Summary" Then ws.Range("D2:D6, D8:D15").Copy W
Sub SummurizeSheets()
Dim ws As Worksheet
Application.ScreenUpdating = False
Sheets("Summary").Activate
For Each ws In Worksheets
If ws.Name <> "Summary" Then
ws.Range("D2:D6, D8:D15").Copy
Worksheets("Summary").Cells(Rows.Count, 4).End(xlUp).PasteSpecial (xlPasteValues)
End If
Next ws
End Sub
Sub summarizesheets()
将ws设置为工作表
Application.ScreenUpdating=False
工作表(“摘要”)。激活
对于工作表中的每个ws
如果ws.Name是“Summary”,那么
ws.Range(“D2:D6,D8:D15”)。复制
工作表(“摘要”).单元格(Rows.Count,4).结束(xlUp).粘贴特殊(xlPasteValues)
如果结束
下一个ws
端接头
模块中,而不是放在
此工作簿
。您可以通过右键单击工作簿名称(从VBA编辑器)并转到插入模块来插入新模块
工作簿()
对象引用工作簿
处理工作表
Sub test()
Dim b2 As Workbook 'We will use this variable as a reference to the external workbook that contains the "Summary" worksheet.
Set b2 = Excel.Workbooks("testbook2") 'We assign the external workbook (which I named "testbook2" for the purposes of this example) to our 'b2' variable.
Dim ws1 As Worksheet
Dim ws2 As Worksheet 'We will use these variables as references to the worksheets we're using.
Set ws1 = Excel.ActiveSheet 'We set ws1 to equal our current sheet (which presumably is where you'll be copying data from).
Set ws2 = b2.Worksheets("Summary") 'We set ws2 to equal the sheet (named "Summary") in the external workbook (named "testbook2").
ws1.Range("D2:D6").Copy 'We copy the data from the active sheet, which we reference using our 'ws1' variable.
'Note: I had issues with using multiple ranges in the same object so I removed this from your code.
ws2.Cells(Rows.Count, 4).End(xlUp).PasteSpecial xlPasteValues 'You only need to use the ws2 variable since we've already defined it as the "Summary" sheet you want.
End Sub
此工作簿时遇到问题
更新
我编辑代码是为了向您展示一个更好的示例。在VBA中几乎不需要使用“激活”或“选择”命令。只需分配变量并直接引用值 有关提示工作簿(“其他工作簿路径”)。工作表(“摘要”)。请参阅。激活。注意:另一个工作簿(我想)需要打开才能工作。我必须给出我的答案。我已经有一段时间没有参考外部的工作手册了,所以我有点生疏了。编辑好的,我测试成功了。我的第一个示例是正确的,我只是没有在同一个Excel实例中打开两个工作簿。对不起,我对此非常陌生。我该如何处理您的代码将其添加到我现有的“结束前”子项中?感谢您的时间和解释,非常感谢。然而,当我运行该模块时,它给出了一条错误消息1004“选择方法无效”,这可能是由于以下几个原因造成的。。。。你知道为什么会这样吗?还有,我如何让它附加来自第二、第三或第四页的数据workbook@Steve我的猜测是,像这样选择多个范围.Range(“D2:D6,D8:D15”)
不能与.Copy
一起使用。另一种方法是使用。选择
将范围分配给“选择”,然后使用选择。复制
复制该选择,但如果我是你,我只会一次复制/粘贴一个范围。这要容易得多,而且你可能不应该把事情复杂化太多,因为你只是在学习。@Steve如果你觉得这个答案有用的话,别忘了把它标对!谢谢