Excel 比较两个非相干数据vba

Excel 比较两个非相干数据vba,excel,vba,find,compare,Excel,Vba,Find,Compare,目前的问题是我必须比较两组数据,一组是初始需求,另一组是完成的表单。 这里的问题是我必须比较两个Id(所有数据都在特定Id下),同一Id下的组件数量也不相同,因为第一个是初始需求,第二个是完成的表单,其中初始表单中的少数项目可能会被跳过 例如: 表1 A、B、C 项目id productcode产品名称 1. tc7868976螺栓 tc7687678螺母 2. tc65799螺钉 tc98908螺栓 第2页 A B C item id produ

目前的问题是我必须比较两组数据,一组是初始需求,另一组是完成的表单。
这里的问题是我必须比较两个Id(所有数据都在特定Id下),同一Id下的组件数量也不相同,因为第一个是初始需求,第二个是完成的表单,其中初始表单中的少数项目可能会被跳过

例如:
表1

A、B、C 项目id productcode产品名称 1. tc7868976螺栓 tc7687678螺母 2. tc65799螺钉 tc98908螺栓 第2页

A B C item id productcode productname 1 tc7868976 bolt 2 tc65799 screw tc98908 bolt tc5657 bolt(1) A、B、C 项目id productcode产品名称 1. tc7868976螺栓 2. tc65799螺钉 tc98908螺栓 tc5657螺栓(1) 现在我必须检查表2中的项目是否在表1中,即使行号不匹配,我该怎么办?
我尝试了循环,但它变得非常乏味,因为数据量非常大,大约有100列。

我已经简化了这个例子-有人能帮我解释一下逻辑吗?

这里有一个例子,当它与项目ID中的产品代码匹配时,只需在列上放置一个真值:

Sub tt()
Dim IR, CS As Variant
Dim column_IR, column_CS As Integer
Dim id_corresp_IR(100000), id_corresp_CS(100000) As Integer

IR = 1 'initial requirements sheet name or number, IR = "Sheet1" is valid also
CS = 2 'Complete sheet name or number, CS = "Sheet2" is valid also

column_IR = 1 'column to match item IDs on the initial requirements Sheet
column_CS = 1 'column to match item IDs on the Complete Sheet
column_p_IR = 2 'column to match product code IDs on the initial requirements Sheet
column_p_CS = 2 'column to match product code IDs on the Complete Sheet
MT_IR = 9 'column to place a Match mark on the initial requirements sheet

NR_IR = Sheets(IR).Cells(Rows.Count, column_IR).End(xlUp).Row 'last row on column_IR to be matched
NR_CS = Sheets(CS).Cells(Rows.Count, column_CS).End(xlUp).Row 'last row on column_CS to be matched
NR_p_IR = Sheets(IR).Cells(Rows.Count, column_p_IR).End(xlUp).Row 'last row on column_p_IR to be matched
NR_p_CS = Sheets(CS).Cells(Rows.Count, column_p_CS).End(xlUp).Row 'last row on column_p_CS to be matched

k = 0

'Compare item id that match, allocates position on array id_corresp_IR and id_corresp_CS, for initial requirements and the complete sheet
For i = 2 To NR_IR
    For j = 2 To NR_CS
        If Sheets(IR).Cells(i, column_IR) = Sheets(CS).Cells(j, column_CS) And Not Sheets(IR).Cells(i, column_IR) = "" Then
            id_corresp_IR(k) = i
            id_corresp_CS(k) = j
            k = k + 1
        End If
    Next j
Next i

'find in which rows each subset of product IDs are located within item IDs that were matched
'and then run a match loop for product IDs
Sheets(IR).Cells(NR_p_IR + 1, column_IR) = "_" 'these will serve as delimitators since we are going to use .End(xlDown) below
Sheets(CS).Cells(NR_p_CS + 1, column_CS) = "_" 'these will serve as delimitators since we are going to use .End(xlDown) below
For Z = 0 To k - 1
    row_ir_bg = id_corresp_IR(Z)
    row_ir_end = Sheets(IR).Cells(id_corresp_IR(Z), column_IR).End(xlDown).Row - 1
    row_cs_bg = id_corresp_CS(Z)
    row_cs_end = Sheets(CS).Cells(id_corresp_CS(Z), column_CS).End(xlDown).Row - 1
    For a = row_ir_bg To row_ir_end
        For b = row_cs_bg To row_cs_end
            If Sheets(IR).Cells(a, column_p_IR) = Sheets(CS).Cells(b, column_p_CS) And Not Sheets(IR).Cells(a, column_p_IR) = "" Then
                Sheets(IR).Cells(a, MT_IR) = True
            End If
        Next b
    Next a
Next Z
Sheets(IR).Cells(NR_p_IR + 1, column_IR) = ""
Sheets(CS).Cells(NR_p_CS + 1, column_CS) = ""

End Sub

不同行上的项目ID是否与产品信息一致(根据您的样本)?如果它们在同一行,它们是沿着列重复,还是后跟空白单元格?输出是什么?也就是说,如果表2中的产品不在表1中,你会怎么做?如果表2中的产品不在表1中,你可以跳过它,没问题,表1是超集,表2是子集,包含ID的列后面是空格,是的,它们在不同的行中,输出是如果表2中的所有项目都在表1中,它应该说true,否则会有很多错误,但这种比较不同于如果它不在同一行,它应该返回false,就像说id1在表1的第2行,id1在表2中的第5行,它应该找到id1,然后比较它下面的元素(同样,它下面的元素不相同,表1中的id 1可能有3个元素,表2中的id 1可能有2个元素,它应该检查表2中的这2个元素是否在表1中,并返回true或false)好的,然后有两个步骤:1)查找哪个“项目id”(Col A)匹配。2) 对于步骤1中匹配的每一个,哪个“产品代码”(Col B)匹配,并将其标记为true。是吗?我会相应地编辑我的代码。不是很干净,但应该这样做。坦克很多,伙计!帮助很大!
Sub tt()
Dim IR, CS As Variant
Dim column_IR, column_CS As Integer
Dim id_corresp_IR(100000), id_corresp_CS(100000) As Integer

IR = 1 'initial requirements sheet name or number, IR = "Sheet1" is valid also
CS = 2 'Complete sheet name or number, CS = "Sheet2" is valid also

column_IR = 1 'column to match item IDs on the initial requirements Sheet
column_CS = 1 'column to match item IDs on the Complete Sheet
column_p_IR = 2 'column to match product code IDs on the initial requirements Sheet
column_p_CS = 2 'column to match product code IDs on the Complete Sheet
MT_IR = 9 'column to place a Match mark on the initial requirements sheet

NR_IR = Sheets(IR).Cells(Rows.Count, column_IR).End(xlUp).Row 'last row on column_IR to be matched
NR_CS = Sheets(CS).Cells(Rows.Count, column_CS).End(xlUp).Row 'last row on column_CS to be matched
NR_p_IR = Sheets(IR).Cells(Rows.Count, column_p_IR).End(xlUp).Row 'last row on column_p_IR to be matched
NR_p_CS = Sheets(CS).Cells(Rows.Count, column_p_CS).End(xlUp).Row 'last row on column_p_CS to be matched

k = 0

'Compare item id that match, allocates position on array id_corresp_IR and id_corresp_CS, for initial requirements and the complete sheet
For i = 2 To NR_IR
    For j = 2 To NR_CS
        If Sheets(IR).Cells(i, column_IR) = Sheets(CS).Cells(j, column_CS) And Not Sheets(IR).Cells(i, column_IR) = "" Then
            id_corresp_IR(k) = i
            id_corresp_CS(k) = j
            k = k + 1
        End If
    Next j
Next i

'find in which rows each subset of product IDs are located within item IDs that were matched
'and then run a match loop for product IDs
Sheets(IR).Cells(NR_p_IR + 1, column_IR) = "_" 'these will serve as delimitators since we are going to use .End(xlDown) below
Sheets(CS).Cells(NR_p_CS + 1, column_CS) = "_" 'these will serve as delimitators since we are going to use .End(xlDown) below
For Z = 0 To k - 1
    row_ir_bg = id_corresp_IR(Z)
    row_ir_end = Sheets(IR).Cells(id_corresp_IR(Z), column_IR).End(xlDown).Row - 1
    row_cs_bg = id_corresp_CS(Z)
    row_cs_end = Sheets(CS).Cells(id_corresp_CS(Z), column_CS).End(xlDown).Row - 1
    For a = row_ir_bg To row_ir_end
        For b = row_cs_bg To row_cs_end
            If Sheets(IR).Cells(a, column_p_IR) = Sheets(CS).Cells(b, column_p_CS) And Not Sheets(IR).Cells(a, column_p_IR) = "" Then
                Sheets(IR).Cells(a, MT_IR) = True
            End If
        Next b
    Next a
Next Z
Sheets(IR).Cells(NR_p_IR + 1, column_IR) = ""
Sheets(CS).Cells(NR_p_CS + 1, column_CS) = ""

End Sub