Excel VBA:使用 for every 循环合并来自 2 个不同文件的行
我有 2 个不同的 Excel 文件。我创建了第三个 .xls
文件,我将在其中编写宏。该宏需要检查第一个 .xls
文件中的所有记录,并且每条记录都应与另一个 .xls
文件中的记录进行比较,如果它们匹配;我应该将整个记录从原始 .xls
文件复制到新的 .xls
文件,并向该记录添加一个包含信息的新单元格。
这应该类似于 VBA 中的内容:
SrcFile1 = path
SrcFile2 = path
for each record in src file1.sheet1
temp valuecell=record.cell(B1)
for each recordToCompare in srcfile2.sheet1
temp valuecell2=recordToCompare.cell(B1)
if(valuecell1==valuecell2) then
ThisWorkbook.thisworksheet.activate
valuecell2.copy
add new cell = value valuecell1.range(A1)
end for each
end for each
如果有人能给我提示或只是一点帮助,我将非常感激!
I have 2 different Excel files. I created a third .xls
file, where I will write the macro. That macro needs to check the data from the first .xls
file for all records, and each record should be compared to the records in the other .xls
file, and if they match; I should copy the whole records from the original .xls
file to the new .xls
file, and add a new cell to that record with information.
This should look something like that in VBA:
SrcFile1 = path
SrcFile2 = path
for each record in src file1.sheet1
temp valuecell=record.cell(B1)
for each recordToCompare in srcfile2.sheet1
temp valuecell2=recordToCompare.cell(B1)
if(valuecell1==valuecell2) then
ThisWorkbook.thisworksheet.activate
valuecell2.copy
add new cell = value valuecell1.range(A1)
end for each
end for each
If anyone could give me a hint or just a bit of help, I would really appreciate!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该可以工作,将其更改为您想要的方式:
您可以创建如下路径:“C:\ExcelFileName.xls”
如果您要比较多列,那么您将需要两个循环,我认为您只是在进行一对一的操作。所以你需要做这样的事情(我会让你为你的应用程序找出正确的逻辑):
Something like this should work, change it to how you want it:
A path you can make as the following: "C:\ExcelFileName.xls"
If you are comparing multiple columns then you will need two loops, I thought you were only doing one to one. So you would need to do something like this (I'll let you work out the correct logic for your application):