Help with error codes ... what am i missing???

rhino4eva

Active Member
Joined
Apr 1, 2009
Messages
260
Office Version
  1. 2010
Platform
  1. Windows
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
For Count = 2 To lastrow
Range("$a$2:a" & lastrow).Find("Sample").EntireRow.Select
Selection.Delete Shift:=xlUp
Next Count
End Sub


I am trying to develop a module to scan thru sheet1 and delete every line where the word "Sample" occurs
I am using the lastrow technique because the length of the sheet is variable. But I get a runtime 91 error that states object variable or with block variable is not set
I don't know where to go with it ... any ideas will be gratefully received
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I think, you are getting error when you don't find the 'Sample'.
try this code instead of loop:
Code:
Sub test()With ActiveSheet
    .AutoFilterMode = False
    With Range("d1", Range("a" & Rows.count).End(xlUp))
        .AutoFilter 1, "*sample*"
        On Error Resume Next
        .Offset(1).SpecialCells(12).EntireRow.Delete
    End With
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
The Filter method does seem like it would work best in the case, but as an FYI for you - If you are using a loop to delete then you need to work backwards through it as the "lastrow" ceases to be which will cause an error.

To give an example of deleting rows with a loop then it needs to be something like;

Code:
Sub DeleteStuff()
lastrow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For Count = lastrow To 1 Step -1

   If Cells(Count, "A").Value = "Sample" Then Rows(Count).Delete
     
Next i

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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