Remove row if value in one cell of the row is duplicated in the same column in the next row

时间:2018-09-18 19:32:01

标签: excel vba

I have a data sheet where column B is a list of data types. The only value I care about is if the value is SIS.

If row 2 has the value SIS in column B and row 3 has the value SIS in column B, then delete row 2. If row 3 contained instead a value of Topic, then keep row 2, ignore row 3, and look at row 4.

The attached image shows the sample data with a column called VBA Instructions. Any help is appreciated.

1 个答案:

答案 0 :(得分:0)

I think the below could work, this is testing if the cell in front has the same value as the current cell, if so then it would delete it. I would copy the dataset into another sheet before running a macro which deletes rows. The only problem with the below code is if you have two "Topic" cells right next to each other, for example if "Topic" tags were in rows 3 and 4 then row 3 would be deleted.

Sub test()


Columns("A:A").Insert Shift:=xlToRight
last = Cells(Rows.Count, 3).End(xlUp).Row


For Each Cell In Range("C2:C" & last)
    If (Cell = Cells(Cell.Row + 1, 3)) = True Then
        Range("A" & Cell.Row).Value = True
    Else:
        Range("A" & Cell.Row).Value = False
    End If
Next Cell

For x = 2 To last:
    If Cells(x, 1) = True Then
        Rows(x).Delete
        x = x - 1
    End If
Next x

Columns("A:A").Delete

End Sub