简体   繁体   中英

How do I use Excel VBA to copy specific cells from two workbooks and combine them into one?

I have two excel workbooks that I am trying to read from. I need to read from a column in the first one, and compare that field to a field in the second workbook. If there is a match, I want to copy the original row to a new excel workbook, and append the name with a piece of another column. Furthermore, if another column in that row is filled, I need to duplicate the original row again into the new workbook, and append that name. That will happen up to 20 times per row, but not always.

Basically, I need to do something like this:

Workbook A:

Name         OtherData1     OtherData2
--------     ----------     ----------
Railroad     Data           Data
MailRoute    Data           Data
BoatPath     Data           Data

Workbook B:

Name          rtuTopic[1]   rtuTopic[2]   [.....]   rtuTopic[20]
--------      --------      --------                --------
Railroad      Route_01      Route_05                Route_21
MailRoute     Route_12      NULL                    NULL
BoatPath      Route_01      Route_15                NULL

Workbook C (the result I want)

Name            OtherData1     OtherData2     rtuTopic
--------        ----------     ----------     --------
Railroad_01     Data           Data           Route_01
Railroad_05     Data           Data           Route_05
...........
Railroad_21     Data           Data           Route_21
MailRoute_12    Data           Data           Route_12
BoatPath_01     Data           Data           Route_01
BoatPath_15     Data           Data           Route_15

The columns I need to append are not conveniently next to one another. They are separated by 5 or 6 other columns, give or take, and I don't know where to get started. I searched around and saw how to copy entire sheets from one workbook to another, but I didn't see how to really specifically dig down and duplicate a row the way I need to.

It seems more like you are asking for a solution to be written for you rather than asking a specific question but here are some pointers to get you going:

Assuming the other workbooks are already open you can refer to the otherbooks using the workbooks list ie

Dim workBookA As Workbook
Dim workBookB As Workbook
Dim workBookC As Workbook

Set workBookA = Workbooks("Workbook A")
Set workBookB = Workbooks("Workbook B")
Set workBookC = Workbooks("Workbook C")

The sheets in those workbooks can be referenced using the name of the sheet ie if you want a sheet called "Main" on Workbook A then you could use:

workBookA.Sheets("Main").Activate

Copying one column from the sheet to one call "Report" on Workbook C could be done like:

workBookA.Sheets("Main").Range("a1", workBookA.Sheets("Main").Range("a1").End(xlDown)).Copy
workBookC.Sheets("Report").Range("a1").PasteSpecial

or if this is easier to follow

workBookA.Sheets("Main").Activate
Range("a1", Range("a1").End(xlDown)).Copy
workBookC.Sheets("Report").Activate
Range("a1").PasteSpecial

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM