0

手头的问题是我必须比较两组数据,一组是初始需求,另一组是完成的表格。
这里的问题是我必须比较两个IDS(所有数据都在特定ID下)并且相同ID下的组件数量也不相同,因为第一个是初始要求,第二个是完成的表格,其中有几个项目来自最初的工作表可能会被跳过。

例如:
表 1

美国广播公司
商品编号 产品代码 产品名称
1
           tc7868976 螺栓
           tc7687678 螺母
2
           tc65799螺丝
           tc98908 螺栓

表 2

美国广播公司
商品编号 产品代码 产品名称
1
           tc7868976 螺栓
2
           tc65799螺丝
           tc98908 螺栓
           tc5657 螺栓(1)

现在我必须检查表 2 中的项目是否在表 1 中,即使行号不匹配我该怎么办?
我尝试了 for 循环,但它变得非常乏味,因为数据量非常高,大约 100 列。
我已经简化了这个例子——有人会帮我解决这个逻辑吗?

4

1 回答 1

0

这是一个示例,当它与项目 ID 中的产品代码匹配时,它只会在列上放置一个 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
于 2013-07-02T13:59:31.107 回答