Excel macro to delete rows of data based on a condition

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
Range("1:5").EntireRow.Delete
End If
Next
dbfromnewjerseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

McOzCommented:
With the following, you're saying "delete the first five rows of the sheet". There is no reference to the actual row being tested:
Range("1:5").EntireRow.Delete

Open in new window

Also, keep in mind that when you are iterating through rows, if you delete a row it is replaced by the next one in the sheet. So going to the next row after deleting means you are effectively skipping one.
Try something like this instead:
Dim lastrow As Long, I As Long
'get last row
lastrow = Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row
Do While I <= lastrow
If Cells(I, 1) <> "California" Then
Rows(I).Delete
'decrement value of lastrow since we deleted a row
lastrow = lastrow - 1
Else
'increment counter to go to next row
I = I + 1
End If
Loop

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbfromnewjerseyAuthor Commented:
OK. Thank you. I thought by coding Range("1:5") that I was saying the range of data for a particular row was in columns 1 through 5 (i.e. A through F).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.