Copying and Pasting between Workbooks VBA

99封情书 提交于 2019-12-23 06:42:45

问题


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

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.

For example I have 3 workbooks called

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

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


回答1:


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


来源:https://stackoverflow.com/questions/41102348/copying-and-pasting-between-workbooks-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!