这是一个示例,当它与项目 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