Remove Duplicate Cells in a Row

后端 未结 4 2075
醉酒成梦
醉酒成梦 2021-01-16 09:04

Just to clarify : I don\'t want to remove duplicates rows, I want to remove Duplicate Cells within a row

So here\'s a classic address table, and in some row there\'s

4条回答
  •  既然无缘
    2021-01-16 09:57

    Maybe this will solve your problem:

    Sub RemoveDuplicatesInRow()
    
        Dim lastRow As Long
        Dim lastCol As Long
        Dim r As Long 'row index
        Dim c As Long 'column index
        Dim i As Long
    
        With ActiveSheet.UsedRange
            lastRow = .Row + .Rows.Count - 1
            lastCol = .Column + .Columns.Count - 1
        End With
    
        For r = 1 To lastRow
            For c = 1 To lastCol
                For i = c + 1 To lastCol 'change lastCol to c+2 will remove adjacent duplicates only
                    If Cells(r, i) <> "" And Cells(r, i) = Cells(r, c) Then
                        Cells(r, i) = ""
                    End If
                Next i
            Next c
        Next r
    
    End Sub
    

提交回复
热议问题