比较列中的行,如果find匹配,则使用vba比较列的内容

我有一个B列的string大约500行,我有单元格空/填充列A和C列值为M.

我需要一个macros,将列B中的每个单元格与同一列B中的其他单元格进行比较,并find重复项,并将两个重复string的列C与M值进行比较,如果相同,则删除整行中的一个。 即使从C到M值的一列在两个重复string之间是不同的,那么它也不应该做任何事情,离开它们。

这是一个工作表的例子。

ABCDEFGHIJKLM 1.2 SERVER_P RE1 GR5 7.3 PROXY NET Uchk=Udis GR YT_5 4.5 PROXY NET Uchk=Udis GR YT_5 3.5 HT_TIMER GS1 6.7 NET CON V1 G_5 MH1 TY1 M_5 7.8 NET CON V1 G_5 MH1 RE3 M_5 

在上面的例子中,它应该比较B列单元格,所以它发现“PROXY NET”和“NET CON V1”是重复的。 那么它应该比较列C到M两个“PROXY NET”,如果列值相同,则应该删除任何一个“PROXY NET”的整个行。 但是对于“NET CON V1”,不应该删除,因为即使其他列值相同,列H的值也是不同的。

这是我到现在为止

 Dim LRow As Long, Count As Long, rcount As Long Dim matchFoundIndex As Long Dim matchColIndex As Variant Dim iCntr As Long With Sheets("Inputs") 'count the rows till which strings are there LRow = .Cells(.Rows.Count, "B").End(xlUp).Row For iCntr = LRow To 1 Step -1 Count = 0 If Cells(iCntr, 2) <> "" Then matchFoundIndex = Application.Match(Cells(iCntr, 2), Range("B1:B" & LRow), 0) If iCntr <> matchFoundIndex Then Cells(iCntr, 2).Interior.ColorIndex = 3 Cells(matchFoundIndex, 2).Interior.ColorIndex = 3 For rcount = 3 To 13 matchColIndex = Application.Match(Cells(iCntr, rcount), Cells(matchFoundIndex, rcount), 0) If Not IsError(matchColIndex) Then Count = Count + 1 If Count = 11 Then Rows(matchFoundIndex).EntireRow.Delete End If Else rcount = 11 End If Next rcount End If End If Next End With 

问题的输出在工作表“input”中更新,但Excel工作表和VBA编辑器窗口进入不响应,我不知道如果输出正确生成。

有人可以帮助我这个。

原始代码有一个永久循环,其中循环计数器rcount重复设置为11.此外,原始代码试图删除匹配的行,而不是我们正在处理的行…这是有问题的,因为我们正确地向后行通过行,以便我们可以删除我们正在处理的行,并保留后续行的地址。 在下面的代码中, For rcount = 3 To 13循环已经被修改了:简单地说,对于匹配行中的每个单元对,如果它们匹配,则循环继续。 如果循环结束,则rcount将为14,并且行iCntr将被删除…如果循环提前退出(因为单元格不匹配),那么rcount不会是14,并且不会删除行。

 Dim LRow As Long, Count As Long, rcount As Long Dim matchFoundIndex As Long Dim matchColIndex As Variant Dim iCntr As Long With Sheets("Inputs") 'count the rows till which strings are there LRow = .Cells(.Rows.Count, "B").End(xlUp).Row For iCntr = LRow To 1 Step -1 Count = 0 If .Cells(iCntr, 2) <> "" Then matchFoundIndex = Application.Match(.Cells(iCntr, 2), .Range("B1:B" & LRow), 0) If iCntr <> matchFoundIndex Then .Cells(iCntr, 2).Interior.ColorIndex = 3 .Cells(matchFoundIndex, 2).Interior.ColorIndex = 3 For rcount = 3 To 13 If .Cells(iCntr, rcount).Value <> .Cells(matchFoundIndex, rcount).Value Then Exit For End If Next rcount If rcount = 14 Then .Rows(iCntr).EntireRow.Delete End If End If Next End With