Forum Discussion

Samuel Parry's avatar
Samuel Parry
Copper Contributor
Jul 19, 2018

Delete rows in an IF test

I need to delete a row if the value in column B for the current row is equal to the value in the next row.  Can I do this in an IF test?If not, then how?

2 Replies

  • Lorenzo Kim's avatar
    Lorenzo Kim
    Bronze Contributor
    maybe you would like to visit the ff sites:
    https://www.extendoffice.com/documents/excel/815-excel-remove-rows-based-on-cell-value.html
    https://stackoverflow.com/questions/46613878/vba-excel-delete-row-based-on-value-in-column
    https://yodalearning.com/tutorials/how-to-delete-rows-in-excel-based-on-a-condition/
  • Lorenzo Kim's avatar
    Lorenzo Kim
    Bronze Contributor

    Mr. Parry

    I don't know if the IF function can do what you want. 

    Below is a SUB (Mr. Man Fai Chan helped me with this - my way of pay it forward) for you to try.

    test this in another sheet - just in case. (always make a backup copy of your work).

    HTH

     

    column B should be sorted first

    Sub DeleteRowforSameB()
    Dim i As Long
    For i = Cells(Rows.Count, "B").End(xlUp).Row To 2 Step -1
    mdata = Cells(i - 1, "B")
    If Cells(i, "B") = mdata Then Rows(i).Delete
    Next i
    End Sub

     

Resources