endless loop help

J

John

I want to copy values on one page and paste them onto another. Sometimes my
find should find only one entry, other times it should find a few... either
way I am stuck since I added the loop command. Not real familiar with how
the DO and LOOP commands work. here is the code... can someone tell me why
I am in an endless loop

Application.ScreenUpdating = False
ActiveCell.Copy
Range("z1").Select
ActiveCell.PasteSpecial xlPasteValues
Range("j10:m10").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("a1").Select
Dim rngToSearch As Range
Dim wks As Worksheet
Dim rngFound As Range
Dim WhatToFind As Variant

Sheets("Contacts").Select
Set wks = Sheets("Assignments")
Set rngToSearch = Sheets("contacts").Columns(1)
'Set WhatToFind = wks.Range("z1").Value

On Error Resume Next
Set rngFound = rngToSearch.find(what:=wks.Range("z1").Value,
LookIn:=xlValues, lookat:=xlWhole)

If rngFound Is Nothing Then
MsgBox "error"
Else
Do
rngFound.Resize(1, 4).Copy
wks.Select
Range("j9").Select
'Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial xlPasteValues
Loop Until rngFound.Value <> rngFound.Offset(1, 0).Value
'If rngFound.Value = rngFound.Offset(1, 0) Then
Set rngFound = rngToSearch.FindNext
'Else
'GoTo ender:
'End If

End If
ender:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub
 
D

Dick Kusleika

John said:
I want to copy values on one page and paste them onto another.
Sometimes my find should find only one entry, other times it should
find a few... either way I am stuck since I added the loop command.
Not real familiar with how the DO and LOOP commands work. here is
the code... can someone tell me why I am in an endless loop

On Error Resume Next
Set rngFound = rngToSearch.find(what:=wks.Range("z1").Value,
LookIn:=xlValues, lookat:=xlWhole)

If rngFound Is Nothing Then
MsgBox "error"
Else
Do
rngFound.Resize(1, 4).Copy
wks.Select
Range("j9").Select
'Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
Selection.PasteSpecial xlPasteValues
Loop Until rngFound.Value <> rngFound.Offset(1, 0).Value
'If rngFound.Value = rngFound.Offset(1, 0) Then
Set rngFound = rngToSearch.FindNext
'Else
'GoTo ender:
'End If

John: The Find method keeps finding as long as there is something to find.
That is, it loops back to the first found cell. You need to save the
address of the first found cell, then include a test in your Loop statement
to see when you get back there. See here

http://www.dicks-blog.com/archives/2004/03/29/the-find-method/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Endless loop? 24
simple quick question 2
Looping a loop? 3
Find and loop help-multiple columns 4
Loop in Excel 3
VBA for Dependents shortcut menu 0
findnext error in loop 2
Delete rows macro 3

Top