Excel Run time error 1004 Application-defined or Object-defined error

I'm getting the following error when I try to run a routine in an Excel macro:
"Excel Run time error 1004  Application-defined or Object-defined error"

The routine is listed below.  I am attempting to delete all rows/records within the used range except those that have "California" as the value in column "A". Columns "A" through "E" are the only columns in the used range and each cell within that range may or may not contain data (i.e. there are some cells or entire rows within the range that are blank or may contain garbage data).

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                                    <------ GETTING THE ERROR ON THIS LINE
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
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.

Martin LissOlder than dirtCommented:
See if this helps

Dim lastrow As Long, I As Long

'get last row
lastrow = Range("F" & ActiveSheet.Rows.Count).End(xlUp).Row
For I = lastrow to 1 Step -1
    If Cells(I, 1) <> "California" Then                                  
        Rows(I).Delete
    End If
Next

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:
That works. Thank you very much.
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
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 Excel

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.