XL2003 VBA: While...Wend Loop Errors When Executed Twice (Run-time error '91')

TechTank

Board Regular
Joined
Sep 5, 2011
Messages
92
Hi All :)

I found the following code on the internet and have tried to use it more than once in a macro to delete rows that have the value "TRUE" in them. This works for the first instance of the code very successfully and the second until it errors on the second execution when no more "TRUE" cells are found:

Code:
 Option Explicit

Sub Delete_Rows_With_TRUE()

Dim SearchCriteria As String:   SearchCriteria = "TRUE"
Dim CriteriaRow As Long   

[B]### Rest of Code to import data is here but removed for easier reading ###[/B]

ActiveSheet.Range("A1").Activate
    On Error GoTo Next1
    CriteriaRow = Cells.Find(What:=SearchCriteria, After:=ActiveCell, LookIn:=xlFormulas, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=True, SearchFormat:=False).Row
    
    While CriteriaRow > 0
        ActiveSheet.Rows(CriteriaRow & ":" & CriteriaRow).Delete Shift:=xlUp
        On Error GoTo Next1
        CriteriaRow = Cells.Find(What:=SearchCriteria, After:=ActiveCell, LookIn:=xlFormulas, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False).Row
    Wend
    GoTo Next1
    
Next1:

'Delete Obsolete Rows
Range("B:IV").Delete
When my macro runs through and gets to this second While...Wend loop it deletes the rows that have "TRUE" in them but then errors The code for the second While...Wend loops is the same as the first except the exit statements of 'Next2':

Code:
[B]### Rest of Code to import data is here but removed for easier reading ###[/B]

 ActiveSheet.Range("A1").Activate
    On Error GoTo Next2
    CriteriaRow = Cells.Find(What:=SearchCriteria, After:=ActiveCell, LookIn:=xlFormulas, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                    MatchCase:=True, SearchFormat:=False).Row
    
    While CriteriaRow > 0
        ActiveSheet.Rows(CriteriaRow & ":" & CriteriaRow).Delete Shift:=xlUp
        On Error GoTo Next2
        CriteriaRow = Cells.Find(What:=SearchCriteria, After:=ActiveCell, LookIn:=xlFormulas, _
                        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                        MatchCase:=False, SearchFormat:=False).Row
    Wend
    GoTo Next2

Next2:

'Delete Obsolete Rows
Range("C:IV").Delete
The error is:

Code:
Microsoft Visual Basic

Run-time error '91':

Object variable or With block variable not set
I'm quite new to VBA and would really appreciate a pointer or any help someone could give.

Thank you for reading and for your time.

Mark. :)
 
Last edited:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try this:

Code:
Sub Delete_Rows_With_TRUE()
    Dim rFind       As Range
 
    Set rFind = Cells.Find(What:=True, _
                           LookIn:=xlFormulas, _
                           LookAt:=xlWhole)
 
    Do While Not rFind Is Nothing
        rFind.EntireRow.Delete
        Set rFind = Cells.FindNext
    Loop
End Sub
 
Upvote 0
shg:

You're a genius! Works like an absolute charm! Thank you so much.

This forum rocks for helpful and nice people. Thank you all so so much.

Mark.
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,299
Members
452,904
Latest member
CodeMasterX

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