【发布时间】:2017-06-12 13:09:04
【问题描述】:
我已经设法遍历两列以查找一列中的值是否存在于另一列中。
现在下一步是确定两列中存在的值是否在值右侧的单元格中包含相同的值。
期望结果的图片和当前结果应该说明我想要达到的目标。
附:不要被代码混淆,因为我的工作簿中的列位于两个不同的工作表上。
一旦我得到匹配的确认,我已经尝试在列中循环两次,但在这一点上,我只是输了......对不起
Sub loopDb()
Set dbsheet1 = ThisWorkbook.Sheets("Sheet1")
Set dbsheet2 = ThisWorkbook.Sheets("Sheet2")
lr1 = dbsheet1.Cells(Rows.Count, 1).End(xlUp).Row
lr2 = dbsheet2.Cells(Rows.Count, 1).End(xlUp).Row
For x = 2 To lr1
act1 = dbsheet1.Cells(x, 1)
For y = 2 To lr2
act2 = dbsheet2.Cells(y, 1)
If Not dbsheet2.Cells(y, 3).Value = "Match" Then
'Only compare if previoulsy not done or resulted in "No match"
If act2 = act1 Then
dbsheet2.Cells(y, 3).Value = "Match"
If dbsheet2.Cells(y, 3).Value = "Match" Then
For i = 2 To lr1
If dbsheet2.Cells(y, 1).Value = dbsheet2.Cells(i, 1).Value Then
dbsheet2.Cells(y, 4).Value = "Match"
Else
dbsheet2.Cells(y, 4).Value = "No match"
End If
Next i
End If
Else
dbsheet2.Cells(y, 3).Value = "No match"
End If
End If
Next y
Next x
End Sub
【问题讨论】:
-
为什么不在你的逻辑中使用
application.worksheetfunction.match(col1value,col2,0)和AND,所以if match1 AND match2 then