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

Posted on 2014-08-23
Last Modified: 2014-08-23
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
'decrement value of lastrow since we deleted a row
lastrow = lastrow - 1
'increment counter to go to next row
I = I + 1
End If
Question by:dbfromnewjersey
    LVL 44

    Accepted Solution

    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                                  
        End If

    Open in new window


    Author Comment

    That works. Thank you very much.
    LVL 44

    Expert Comment

    by:Martin Liss
    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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now