Application Defined or object defined error - Run time error 1004

vbvba

New Member
Joined
May 25, 2012
Messages
34
Hi Guys,

I'am trying to write a small script to delete the "Cancelled" word in a column and keep the "Shipped" and "delivered" words.

Below is the code

Code:
Sub removecancelled()
Dim st As String, i As Integer, lastrow1 As Long
st = WorksheetFunction.Match("Status", Range("1:1"), 0)
lastrow1 = Cells(Rows.Count, st).End(xlUp).Row
For i = lastrow1 To 2 Step -1
If Cells(i, st) = "cancelled" Then
Cells(i, st).EntireRow.Delete
End If
Next i

However, when I run the script get an error "Application Defined or object defined error - Run time error 1004" in the 4th line

lastrow1 = Cells(Rows.Count, st).End(xlUp).Row

Please reply and help me in finding out the issue in this script.
 
Last edited:

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Here is a different method (no loops) that should work for you (if you have a lot of rows of data, then it should also be faster than the code you now have)...
Code:
Sub removecancelled()
  With Rows(1).Find("Status", , , xlWhole, , , True).EntireColumn
    .Replace "cancelled", "#N/A", xlWhole, , False
    On Error Resume Next
    .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
    On Error GoTo 0
  End With
End Sub
 
Upvote 0
Hi Norie,

Thanks for the reply

I declared st as Long now the error is different "Unable to get the match property of the worksheetfunction class"

Kindly confirm what could be the issue now ?
 
Upvote 0
That suggests 'Status' isn't being found.
 
Upvote 0
Hi Norie,

Oops Status was declared in 2nd row of the sheet so match function was unable to search it in first row.
Thanks for the help
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,708
Messages
6,174,006
Members
452,542
Latest member
Bricklin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top