【问题标题】:compare two worksheets (with different number of rows on each sheet) and to get the list of not matching rows比较两个工作表(每张工作表上的行数不同)并获取不匹配行的列表
【发布时间】:2014-04-29 16:44:18
【问题描述】:

以下是我的要求

1.有两张纸,比如 Sh1、Sh2

2.两张纸之间的行数不同(它们之间的行数可能/可能不同)

3. 表格的已使用列因考虑比较的数据而异 但是在任何给定的时间点,两张纸都将具有相同数量的 Used 列 (理想情况下,两张纸的范围都是动态的)

4.现在我需要对它们进行比较,找出 Sh1、Sh2 中多余的记录,并将它们保存在 diff 工作表 Sh3、Sh4 左右。

5.执行的比较需要是Sh1和Sh2之间的行级比较 6.我更喜欢使用数组(将工作表加载到数组)并对其进行比较并返回值 - 性能起着重要作用,因为数据可能有数百万的记录

  1. 有没有办法比较两个大小不相同的数组?
  2. 可以忽略匹配的行

  3. 有没有办法应用一些 Join() 函数并从 Array1 读取完整的行并将它们与 Array2 进行比较?

对不起,如果我以任何方式违反了论坛规则!

我希望没有讨论比较具有不同动态范围的两张工作表以找到这里或任何地方的不匹配

我主要看到 ppl 将比较限制为仅一个 Column ,或仅用于固定范围

我的最终目标是编写一些 VBA 代码,其操作类似于“超越比较”(好吧,这不是确切的行为,请阅读它)

【问题讨论】:

  • 将这些文件导入 MS Access 是否适合您?它非常适合这种情况。

标签: arrays vba compare array-difference


【解决方案1】:

我一直在研究一种类似的 vba 宏。 希望下面的代码有帮助!

您只需要图片并包含 1)如果可能的话,行级比较 2)遍历数组并找出差异

** 我是

代码如下:

'比较代码

Sub CompareMacro()

Application.ScreenUpdating = True
Dim sheet1, sheet2, sheet4, sheet3 As Worksheet
Dim rComp, rcomp1 As Range, addy As String, addz As String
Dim iRow As Long, jCol As Long
Dim kRow As Long, lCol As Long
Dim strFileRange As String
Dim strDBRange As String



Set sheet1 = Sheets("File")
Set sheet2 = Sheets("DB")
Set sheet3 = Sheets("File(-)DB")
Set sheet4 = Sheets("DB(-)File")


sheet1.Select
Set rComp = sheet1.UsedRange

sheet2.Select
Set rcomp1 = sheet2.UsedRange


addy = rComp.Address
addz = rcomp1.Address
ary1 = rComp
ary2 = rcomp1
ary3 = sheet3.Range(addy)
ary4 = sheet4.Range(addz)

'*********File MINUS DB code goes here*********

'Step 1
'VALIDATE IF THE ROW1 OF File matches with Any of the Row in DB
'This step should include Iteration of Rows in DB range


'Step 2
'Proceed with Next row (ROW1+1 OF File) if Match Occurs
'This step contains incremental of File row & Starting comparison from First Row of DB

'Step 3
'If no Match occurs , Add the Specific Row to ary3
'This step captures the complete Mismatch record of File Row


'*********DB MINUS File code goes here*********


'Similar to the Flow of File MINUS DB



'adding the Array3 & 4 resultant Sheets

sheet3.Range(addy) = ary3
sheet4.Range(addz) = ary4

结束子

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多