excel vba DO UNTIL和IF,比较string单元格

Sub比较()
j = 0

For i = 0 To 1109 Do Until Cells(2 + i, 3) = Cells(2 + j, 12) i = i + 1 Loop If Cells(2 + i, 4) = Cells(2 + j, 13) Then Cells(2 + j, 16) = "OK" End If i = 0 j = j + 1 Next i End Sub 

如果列DM中的单元格是相同的(用If指令),那么只要列CL中的相关名称相同(用Do Until指令),我想写一个“OK”。 所以我想做一个比较。
但单元格中的名称( Do Until循环内部)是由string形成的。 Excel给了我一个关于DO UNTIL的句子的错误。
你可以帮帮我吗?

更新:

以下是一些示例数据:

在这里输入图像说明

这是我最终会想要的东西enter image description here

对于同一个CELLS NAME(列A和J),查看条件1(列B和K)的值,find每个匹配,最后写入列P(和箭头等于模型2中匹配的那个)或OK(如果cond1的值相同)或NO(如果cond1的值不同)

如果你想逐行比较列D / MC / L或者如果你真的试图比较所有的行,我不太确定。 因此,我调整了两种情况下的代码:

 Option Explicit Sub ComparisonRowByRow() Dim i As Long With ThisWorkbook.Worksheets("Sheet1") For i = 0 To 1109 If .Cells(2 + i, 3).Value2 = .Cells(2 + i, 12).Value2 And _ .Cells(2 + i, 4).Value2 = .Cells(2 + i, 13).Value2 Then .Cells(2 + i, 16).Value2 = "OK" End If Next i End With End Sub 

 Sub CompareAllWithAll() Dim i As Long, j As Long Dim lngLastRow As Long With Application .EnableEvents = False .Calculation = xlCalculationManual .ScreenUpdating = False End With With ThisWorkbook.Worksheets("Sheet1") lngLastRow = .Cells.Find("*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row For i = 0 To 1109 .Cells(2 + i, 16).ClearContents For j = 2 To lngLastRow If .Cells(2 + i, 3).Value2 = .Cells(2 + j, 12).Value2 And _ .Cells(2 + i, 4).Value2 = .Cells(2 + j, 13).Value2 Then If .Cells(2 + i, 16).Value2 = vbNullString Then .Cells(2 + i, 16).Value2 = "OK, match found in row " & j Else .Cells(2 + i, 16).Value2 = .Cells(2 + i, 16).Value2 & ", " & j End If End If Next j Next i End With With Application .Calculation = xlCalculationAutomatic .ScreenUpdating = True .EnableEvents = True End With End Sub 

如果你真的想要比较所有的行,那么它可能也是重要的,find匹配的行。 因此,我添加到代码来logging匹配。

请记住,第二sub目前是相当耗时的。 如果你有1,109行,那么代码将运行1,109 * 1,109次。 这相当于超过120万次迭代:比较D2与M2,M3,M4,M5,… M1109,比较D3与M2,M3,M4,M5,M6,… M1109等。

为了使这个过程更快,我closures了ScreenUpdatingEventsCalculation 。 然而, arrays有更好/更快的方式。 但是我不太清楚你是否熟悉数组,所以我保留了原来的编码。 毕竟,我们试图帮助您使用您的代码(将来您将不得不更新和维护)。