简体   繁体   English

如何使用 Excel VBA 从两个工作簿中复制特定单元格并将它们合并为一个?

[英]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.我有两本要阅读的 excel 工作簿。 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.如果有匹配项,我想将原始行复制到新的 excel 工作簿,并将名称附加到另一列的一部分。 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.每行最多会发生 20 次,但并非总是如此。

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.他们被5或6个其他列隔开,给予或接受,我不知道从哪里开始。 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:可以使用工作表的名称引用这些工作簿中的工作表,即如果您想要工作簿 A 上名为“Main”的工作表,那么您可以使用:

workBookA.Sheets("Main").Activate

Copying one column from the sheet to one call "Report" on Workbook C could be done like:可以将工作表中的一列复制到工作簿 C 上的一个调用“报告”中,如下所示:

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祝你好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel VBA 在两个工作簿之间复制单元格 - Excel VBA copy cells between two workbooks excel vba宏,以匹配来自两个不同工作簿的单元格并相应地复制和粘贴 - excel vba macro to match cells from two different workbooks and copy and paste accordingly 如何在Excel中的两个工作簿之间进行复制? - How do I copy between two workbooks in Excel? VBA 将多个工作簿中的一个单元格复制到另一个工作表中的特定单元格中 - VBA to copy a cell from multiple workbooks into specific cells in the another sheet excel vba宏,以匹配来自两个不同工作簿的单元格,并相应地进行复制和粘贴,并且只需要更新空白单元格 - excel vba macro to match cells from two different workbooks and copy and paste accordingly and have to update only the empty cells 比较两个单元格并复制它们/2 个工作簿 - Compare two cells and copy them / 2 workbooks 打开两个Excel工作簿(每个都有VBA代码)时,如何在工作簿之间切换代码视图? - When two Excel workbooks are open, each with VBA code, how do I switch the code view between workbooks? 将单元格从一个工作簿复制到多个工作簿excel - Copy cells from one workbook into multiple workbooks excel 在 Excel 中,如何使用 VBA 仅复制具有特定顺序的所需单元格 - In Excel how do I copy only wanted cells with specific order with VBA 如何使用excel VBA以特定方式复制和粘贴单元格,然后删除某些行? - How do I copy and paste cells and then delete certain rows in a specific way using excel VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM