Excel VBA:使用 for every 循环合并来自 2 个不同文件的行

发布于 2024-12-10 21:01:11 字数 697 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅我要高飞 2024-12-17 21:01:11

像这样的东西应该可以工作,将其更改为您想要的方式:

SrcFile1 = "path"
SrcFile2 = "path2"

dim i as long
dim wkb1 as workbook, wkb2 as workbook
dim wks1 as worksheet, wks2 as worksheet
dim v1 as variant, v2 as variant

'Get workbooks
set wkb1=getobject(srcfile1)
set wkb2=getobject(srcfile2)
'Get worksheets
set wks1=wkb1.worksheets("Sheet1")
set wks2=wkb2.worksheets("Sheet1")
'Get data - transpose single column data so array will be one dimensional
v1=application.transpose(wks1.range("A1:A" & wks1.range("A65000").end(xlup).row))
v2=application.transpose(wks2.range("A1:A" & ubound(v1)))

for i=1 to ubound(v1)
  if v1(i)<> v2(i) then
    'Copy over different data, don't know how many columns you have, 
    ' you can also do a .end(xlleft) to get the last column
    wks2.range("A" & i & ":Q" & i)=wks1.range("A" & i & ":Q" & i)
    wks2.range("R" & i)= "Information here"
  end if
next i

您可以创建如下路径:“C:\ExcelFileName.xls”
如果您要比较多列,那么您将需要两个循环,我认为您只是在进行一对一的操作。所以你需要做这样的事情(我会让你为你的应用程序找出正确的逻辑):

dim j as long

v1=wks1.range(cells(1,1), cells(wks1.range("A65000").end(xlup).row _
   , wks.range("IV1").end(xlLeft))
v2=wks2.range(cells(1,1),cells(ubound(v1),ubound(v1,2))

for i=1 to ubound(v1)
  for j=1 to ubound(v1,2)
    if v1(i,j)<> v2(i,j) then
      wks2.range(cells(i,1),cells(i,ubound(v1,2))) _
           =wks1.range(cells(i,1),cells(i,ubound(v1,2)))
      wks2.range(cells(i,ubound(v1,2)+1))= "Information here"
      exit for

Something like this should work, change it to how you want it:

SrcFile1 = "path"
SrcFile2 = "path2"

dim i as long
dim wkb1 as workbook, wkb2 as workbook
dim wks1 as worksheet, wks2 as worksheet
dim v1 as variant, v2 as variant

'Get workbooks
set wkb1=getobject(srcfile1)
set wkb2=getobject(srcfile2)
'Get worksheets
set wks1=wkb1.worksheets("Sheet1")
set wks2=wkb2.worksheets("Sheet1")
'Get data - transpose single column data so array will be one dimensional
v1=application.transpose(wks1.range("A1:A" & wks1.range("A65000").end(xlup).row))
v2=application.transpose(wks2.range("A1:A" & ubound(v1)))

for i=1 to ubound(v1)
  if v1(i)<> v2(i) then
    'Copy over different data, don't know how many columns you have, 
    ' you can also do a .end(xlleft) to get the last column
    wks2.range("A" & i & ":Q" & i)=wks1.range("A" & i & ":Q" & i)
    wks2.range("R" & i)= "Information here"
  end if
next i

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):

dim j as long

v1=wks1.range(cells(1,1), cells(wks1.range("A65000").end(xlup).row _
   , wks.range("IV1").end(xlLeft))
v2=wks2.range(cells(1,1),cells(ubound(v1),ubound(v1,2))

for i=1 to ubound(v1)
  for j=1 to ubound(v1,2)
    if v1(i,j)<> v2(i,j) then
      wks2.range(cells(i,1),cells(i,ubound(v1,2))) _
           =wks1.range(cells(i,1),cells(i,ubound(v1,2)))
      wks2.range(cells(i,ubound(v1,2)+1))= "Information here"
      exit for
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文