匹配包含重复单元格的行

时间:2020-06-23 13:48:44

标签: excel vba

我认为,线程“连续删除重复的单元格”提供了用于上述目的的有效语法(标题)。我有动力去执行漂亮的代码。但是,我的细微修改以某种方式破坏了代码:

    wsR.Range("E1").Value = "Match"
    
    Dim lR As Long
    Dim lC As Long
    Dim k As Long 'row index
    Dim c As Long 'column index
    Dim i As Long
        
    With wsR.UsedRange
        lR = .Row + .Rows.Count - 1
        lC = .Column + .Columns.Count - 1
    End With
        
    For k = 2 To lR
        For c = 1 To lC
            For i = c + 1 To lC 'change lastCol to c+2 will remove adjacent duplicates only
                If wsR.Cells(k, i) <> "" And wsR.Cells(k, i) = wsR.Cells(k, c) Then
                    wsR.Cells(k, "E") = "Unmatched"
                    Else
                    wsR.Cells(k, "E") = "Matched"
                    
                End If
            Next i
        Next c
    Next k

请注意,我尝试为每个正行将字符串“匹配”插入E列。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

    wsR.Range("E1").Value = "Match"
    
    Dim lR As Long
    Dim lC As Long
    Dim k As Long 'row index
    Dim c As Long 'column index
    Dim i As Long
        
    With wsR.UsedRange
        lR = .Row + .Rows.Count - 1
        lC = .Column + .Columns.Count - 1
    End With
        
    For k = 2 To lR
        For c = 1 To lC
            For i = c + 1 To lC 'change lastCol to c+2 will remove adjacent duplicates only
                If wsR.Cells(k, i) <> "" And wsR.Cells(k, i) = wsR.Cells(k, c) Then
                    wsR.Cells(k, "E") = "Unmatched"
                    Else
                    wsR.Cells(k, "E") = "Matched"
                    
                End If
            Next i
        Next c
    Next k