Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I End a Loop from inside the Loop?

Status
Not open for further replies.

tudor30

Technical User
Jan 7, 2004
49
US
I'm using this code to look up the column from a variable cell position. When I find the matching value I would like the Loop to End.

r = 1
Do

If .Cells(lngRow - r, 16) = strCode Then
lngLevel = .Cells(lngRow - r, 2).Value
'Place statement here to exit the Loop
ElseIf .Cells(lngRow - r, 16) <> strCode Then
r = r + 1
End If

Loop
 
r=1
do until Cells(lngRow - r, 16) = strCode
r = r + 1
loop
lngLevel = .Cells(lngRow - r, 2).Value
 
Why use a loop for this at all?

If you have a lot of data (even if you don't!) using the Find method will be much faster

Code:
lngLevel = Range("A1:P" & lngRow).Find(what:=strCode, searchorder:=xlByRows, _
    searchdirection:=xlPrevious).Offset(-1, -14)

Happy Friday
;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Slight error on the code above [blush]

It should read
Code:
Range("[b]P[/b]1:P" & lngRow) etc...

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top