Exiting Loop on Condition

SBF12345

Well-known Member
Joined
Jul 26, 2014
Messages
614
I would like to be able to exit the interior loop if the line " If TestVar <> "" Then" is equal to any value. I am receiving a compile error on this line
Code:
Rows(bb).EntireRow.Delete Exit Do

Is there some way I can exit the loop if TestVar comes back as not-nothing and then proceed within the outer loop?

Code:
Dim bb As Integer:          bb = Range("C5").End(xlDown).RowDim dd As Integer:          dd = 1
Dim Spacer As Integer:      Spacer = NumObs + 1
Dim StartDate1 As Date
Dim EndDate1 As Date
Dim StartDate2 As Date
Dim EndDate2 As Date
Dim Range1 As Range
Dim Range2 As Range
Dim TestVar As Range


Do Until bb = 5


    StartDate1 = Cells(bb, 4)
    EndDate1 = Cells(bb, 5)
    Range1 = Range(StartDate1, EndDate1)
    
    Do Until bb - dd = 5
    
        StartDate2 = Cells(bb - dd, 4)
        EndDate2 = Cells(bb - dd, 5)
        Range2 = Range(StartDate2, EndDate2)
        
        TestVar = Application.Union(Range1, Range2)
        
        If TestVar <> "" Then
        
        Rows(bb).EntireRow.Delete Exit Do
    
        Else: dd = dd - 1[QUOTE][/QUOTE]

    


        Loop
    
        End If
        
bb = bb - 1
 
Loop
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
You need to split that onto 2 lines.
It also looks like your End If is in the wrong place.
 
Upvote 0
Is there a reason why you have both on the same line.

Try taking the "Exit do" to the next line or separating with a colon,
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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