Cutting a "Do Until" loop once it finds the result.

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hello all,

This is just out of curiosity, but I have a "stop" on my Do Until loop which says when A is "", stop.

It needs to run down a large list until it finds a criteria, then it saves the contents of whatever cell is in column S as a variable.

Is there a way to halt it once it finds the variable? Because according to this loop:


Code:
Do Until Cells(ActiveCell.Row, "A").Value = ""    If Cells(ActiveCell.Row, "A").Value = PapNam Then
        If Cells(ActiveCell.Row, "B") = "JGRT" Then
        JGROI = Cells(ActiveCell.Row, "S").Value
        Else
        OMROI = Cells(ActiveCell.Row, "S").Value
        End If
    End If
ActiveCell.Offset(1, 0).Activate
Loop


Even when it finds that A = PapNam and B is JGRT, and it saves the number in S as a variable for JGROI, it will still loop all the way down 1,000 papers looking for any other instances of the PapNam, even though it's already got the information needs.


Can I say for instance, Do Until Cells(ActiveCell.Row, "A").Value = "" or Variables = Filled


Cheers
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Just add "Exit Do" if you want to bail early.

Also, don't use "Select" and "Activate" statements in your loops. Loops are slow and inefficient enough as it is, adding those things can really slow it down.
Identify the range you want to loop through, and loop through without selecting, i.e. structure it like this instead.
Code:
Dim cell as Range
Dim rng as Range

Set rng=Range(whatever range you want)

For each cell in rng
    ...
Next cell
To exit that loop early, you would just use an "Exit For".
 
Last edited:
Upvote 0
Just add "Exit Do" if you want to bail early.

Also, don't use "Select" and "Activate" statements in your loops. Loops are slow and inefficient enough as it is, adding those things can really slow it down.
Identify the range you want to loop through, and loop through without selecting, i.e. structure it like this instead.
Code:
Dim cell as Range
Dim rng as Range

Set rng=Range(whatever range you want)

For each cell in rng
    ...
Next cell
To exit that loop early, you would just use an "Exit For".


Hi Joe,

So in that example, where the "..." is I would have if "cells(activecell.row, "A").value = PapNam Then" Right? That's where it goes?
 
Upvote 0
Why not forgo the loop altogether and use Match?
Code:
Res = Application.Match(PapName, Range("A" & ActiveCell.Row & ":A" & Range("A" & Rows.Count).End(xlUp).Row), 0)

If Not IsError(Res) Then
    If Cells(Res, "B") = "JGRT" Then
        JGROI = Cells(Res, "S").Value
    Else
        OMROI = Cells(Res, "S").Value
    End If
End If
 
Upvote 0
So in that example, where the "..." is I would have if "cells(activecell.row, "A").value = PapNam Then" Right? That's where it goes?
That is where your checks would go, but you wouldn't use ActiveCell. It would look like:
Code:
If cell.value = PapNam Then
(assuming that you have defined "rng" to be cells in column A)
 
Upvote 0

Forum statistics

Threads
1,215,767
Messages
6,126,766
Members
449,336
Latest member
p17tootie

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