Excel macro to delete rows of data based on a condition
Posted on 2014-08-23
I have the following routine in an Excel macro that I'm trying to use to delete all rows in the active range where the value in column "A" is NOT equal to "California". In other words, I'm trying to retain all records where the value in column "A" equals "California" and delete all other rows. For some reason, the routine is deleting everything including the "California" records. There are definitely records with the value of "California" in column "A"; as I did a test run using a messagebox to display what is in the cell. What am I doing wrong? By the way, the only columns containing data are columns "A" through "E" ( 5 columns ).
Dim lastrow As Long, I As Long
lastrow = Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row
For I = 1 To lastrow
If Cells(I, 1) <> "California" Then