[英]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.