在工作簿 VBA 之间复制和粘贴

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41102348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 11:41:43  来源:igfitidea点击:

Copying and Pasting between Workbooks VBA

excelvbaexcel-vbacopy-paste

提问by Shah Miah

Please bear with me I am still learning VBA and could do with a little help.

请耐心等待,我仍在学习 VBA,需要一点帮助。

I am trying to copy data from one workbook to another even though there I have found many answers on this I am unable to Understand the code as its quite complicated. I was hoping if someone could help me with a basic code which is pretty easy to read and understand.

我正在尝试将数据从一个工作簿复制到另一个工作簿,尽管我在这方面找到了很多答案,但我无法理解代码,因为它非常复杂。我希望是否有人可以帮助我编写一个非常易于阅读和理解的基本代码。

I am currently looking to copy all data off 3 different workbooks and paste it into 1 workbook across 3 worksheets.

我目前希望从 3 个不同的工作簿中复制所有数据,并将其粘贴到 3 个工作表中的 1 个工作簿中。

For example I have 3 workbooks called

例如,我有 3 个工作簿,称为

AA BB CC

AA BB CC

I want to copy all the data (value only) from these workbooks and paste then into workbook called

我想从这些工作簿中复制所有数据(仅值),然后粘贴到名为的工作簿中

Main

主要的

But I want that data from AA.to go into Main Worksheet1 and But I want that data from BB to go into Main Worksheet2 and But I want that data from CC to go into Main Worksheet3

但我希望来自 AA.to 的数据进入 Main Worksheet1,但我希望来自 BB 的数据进入 Main Worksheet2,但我希望来自 CC 的数据进入 Main Worksheet3

I hope I have explained this question properly and I appreciate any help.

我希望我已经正确解释了这个问题,我感谢任何帮助。

回答by Brad

As you have supplied no code, this should be sufficient enough to get you started. You'll need to edit this and fix this to suite your needs.

由于您没有提供任何代码,这应该足以让您入门。您需要编辑它并修复它以满足您的需求。

Sub test()
    Dim Wb1 As Workbook, Wb2 As WorkBook, Wb3 As Workbook
    Dim MainBook As Workbook

    'Open All workbooks first:
    Set Wb1 = Workbooks.Open(" path to copying book ")
    Set Wb2 = Workbooks.Open(" path to copying book ")
    Set Wb3 = Workbooks.Open(" path to copying book ")
    Set MainBook = Workbooks.Open(" path to destination book ")

    'Now, copy what you want from wb1:
    wb1.Sheets("Sheet1").Cells.Copy
    'Now, paste to Main worksheet:
    MainBook.Sheets("Sheet1").Range("A1").PasteSpecial

    'Now, copy what you want from wb2:
    wb2.Sheets("Sheet1").Cells.Copy
    'Now, paste to Main worksheet:
    MainBook.Sheets("Sheet2").Range("A1").PasteSpecial

    'Now, copy what you want from wb3:
    wb3.Sheets("Sheet1").Cells.Copy
    'Now, paste to Main worksheet:
    MainBook.Sheets("Sheet3").Range("A1").PasteSpecial

    'Close Wb's:
    Wb1.Close
    Wb2.Close
    Wb3.Close
    MainBook.Save
    MainBook.Close

End Sub