Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将附加数据复制到其他工作表_Vba_Excel - Fatal编程技术网

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编辑器)并转到插入模块来插入新模块
  • 确保要使用/参考的工作簿已打开
  • 确保所有工作簿都位于同一工作簿集合中。除非手动创建其他Excel实例,否则这不是问题
  • 像您一样,使用
    工作簿()
    对象引用工作簿 处理工作表

    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如果你觉得这个答案有用的话,别忘了把它标对!谢谢