在A到E列中有数据,我想做的是比较E列中的值,当A、B、C和D列相同时,如果满足这些条件,将X值添加到F列的行中。我只想标记E列中不匹配的行,但是当我标记一行时,应该标记所有具有相同A、B、C和D值的行。
例如,如果前三行的A、B、C和D相等,我想比较这些条目。由于E3与E1或E2相比存在不匹配,我希望F1通过F3显示为X,因为A、B、C和D在第4行和第5行中相等,但是E4和E5匹配,我希望它忽略这些单元格。
我现在拥有的是:
Sub Compare()
Worksheets(Sheet1).Activate
For w = 1 To Sheet1.Cells(Rows.Count, "A").End(xlUp).Row
For v = -10 To 10
If Sheet1.Cells(w, "A").Value = Sheet1.Cells(w + v, "A").Value And Sheet1.Cells(w, "B").Value = Sheet1.Cells(w + v, "B").Value And Sheet1.Cells(w, "C").Value = Sheet1.Cells(w + v, "C").Value And Sheet1.Cells(w, "D").Value = Sheet1.Cells(w + v, "D").Value And Sheet1.Cells(w, "E").Value <> Sheet1.Cells(w + v, "E").Value Then
Sheet1.Cells(w, "F").Value = "X"
End If
Next v
Next w
End Sub
但它跑不了。我需要改变什么才能让它起作用?
发布于 2018-10-13 03:06:45
变化很大。在您的问题中,还讨论了检查3行,所以我设置了一个变量vrange=3
Sub Compare()
Dim crit As Boolean
vrange = 3
'Sheet1.Activate
Endi = Sheet1.Cells(1, 1).End(xlDown).Row
For r = 1 To Endi
vmin = WorksheetFunction.max(1, r - vrange) - r
vmax = WorksheetFunction.min(Endi, r + vrange) - r
For v = vmin To vmax
crit = True
For c = 1 To 4
crit = crit And Sheet1.Cells(r, c).value = Sheet1.Cells(r + v, c).value
Next c
If crit And Sheet1.Cells(r, 5).value <> Sheet1.Cells(r + v, 5).value Then
Sheet1.Cells(r, 6).value = "X"
End If
Next v
Next r
End Sub
https://stackoverflow.com/questions/52747845
复制相似问题