Loop that tests and stops if condition is met

Will85

Board Regular
Joined
Apr 26, 2012
Messages
240
Office Version
  1. 365
Platform
  1. Windows
My current loop is this:

Code:
Let x = 0
Do While x < 7
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Resize(Selection.Rows.Count, Selection.Columns.Count + 8).Select
    Selection.ClearContents
    Selection.End(xlDown).Select
    x = x + 1
Loop

I would prefer to not have to define the loop, ie 7 iterations, and instead have it test for a condition.

After the first iteration, I want the loop to go to the left one column and down two rows from the last active cell, and if this cell contains the value "x" stop the loop, if it doesn't, then the activecell needs to get back to where it left off before testing for "x", ie one column to the right and two rows up, and then move on to the next iteration of the loop and continue testing until it finds x.
 
Last edited by a moderator:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
If it doesnt find an x in B7, then yes, move back to C5 and continue the loop until it finds an x
 
Upvote 0
My current loop is this:

Code:
    Let x = 0
Do While x < 7
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Resize(Selection.Rows.Count, Selection.Columns.Count + 8).Select
    Selection.ClearContents
    Selection.End(xlDown).Select
    x = x + 1
Loop

I would prefer to not have to define the loop, ie 7 iterations, and instead have it test for a condition.

After the first iteration, I want the loop to go to the left one column and down two rows from the last active cell, and if this cell contains the value "x" stop the loop, if it doesn't, then the activecell needs to get back to where it left off before testing for "x", ie one column to the right and two rows up, and then move on to the next iteration of the loop and continue testing until it finds x.

Hi,
According to what you described you'll be looping 7 iterations between two cells only, ex active cell c5, then move to B7, if no x back to c5 and back to b7. Is that what you want?

Regards,
Sebastian
 
Upvote 0
No, I want it to execute some code, then test for x, if it finds x, stop, if not then execute my code again, and so on until it finds x, so:

Step 1:

Code:
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Resize(Selection.Rows.Count, Selection.Columns.Count + 8).Select
    Selection.ClearContents
    Selection.End(xlDown).Select

Step 2:

Go down two rows, and to the left one column from the active cell, if the value in this cell is "x", then stop if not, move on to step 3

Step 3:

Go to the right one column, up two rows<now were="" back="" to="" the="" last="" active="" cell


Then repeat Step 1, 2, 3. . .</now>
 
Last edited:
Upvote 0
Check this, if that's what you're looking for.

regards,
Sebastian
Code:
 Sub LoopingTest()
    Dim xFound As Boolean
    Const VALUE_TO_FIND = "x"
    xFound = False
    
    Do While xFound = False
        Selection.End(xlDown).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Resize(Selection.Rows.Count, Selection.Columns.Count + 8).Select
        Selection.ClearContents
        Selection.End(xlDown).Select
        If LCase(ActiveCell.Offset(2, -1)) = VALUE_TO_FIND Then
            xFound = True
        End If
    Loop
End Sub
 
Upvote 0
You're Welcome! ☺
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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