How do I ignore the first row in an Excel macro that deletes all rows based on a criteria?

后端 未结 3 1166
终归单人心
终归单人心 2020-12-02 02:54

This macro is designed to compare the data in column C and D and if C does not match D in a certain row, it deletes the entire tow. The problem is that it deletes the header

3条回答
  •  死守一世寂寞
    2020-12-02 03:18

    you can avoid loops:

    Sub deleteNonMatchingRows()
        With Range("C2", Cells(Rows.Count, "C").End(xlUp)) ' reference column C cells from row 2 doen to last not empty one
            With .Offset(, .Parent.UsedRange.Columns.Count) ' reference referenced range offset by active sheet used range columnns (to be sure you'r not overwriting already filled cells)
                .FormulaR1C1 = "=IF(RC3<>RC4,1,"""")" ' have referenced cells show a "1" if corresponding cells in column C and D match
                .SpecialCells(xlCellTypeFormulas, xlNumbers).EntireRow.Delete ' delete all rows whose referenced column formula result is a number
                .ClearContents ' clear referenced range
            End With
        End With
    End Sub
    

提交回复
热议问题