My Search keeps looping

NessPJ

Active Member
Joined
May 10, 2011
Messages
420
Office Version
  1. 365
Hi,

My search keeps looping. I found some other threads/tips to try and counter that by checking the first found address against the next one, but it does not seem to work.

Can anyone tell me what i am doing wrong please?

VBA Code:
    Dim Found As Range
    Dim FoundRegel As Long
    Dim FirstFoundAddr As String
    Dim ActiveFoundCell As Range
    Set SearchRange = Range("A:AZ")
       
    With SearchRange
    Set Found = Cells.Find(What:="AAA", LookIn:=xlFormulas, LookAt:=xlPart)
    If Not Found Is Nothing Then
    FirstFoundAddr = Found.Address
    FoundRegel = Found.Row
    End If
        Do Until Found Is Nothing
            If PlanWb.Sheets(CurSheet).Range("P" & FoundRegel).Value = 1 Then
            PlanWb.Sheets(CurSheet).Range("R" & FoundRegel).Value = 10
            PlanWb.Sheets(CurSheet).Range("S" & FoundRegel).Value = "Checked"
            PlanWb.Sheets(CurSheet).Range("Z" & FoundRegel).Value = "0:15"
            End If
        Set ActiveFoundCell = .FindNext(After:=Found)
            If ActiveFoundCell.Address = FirstFoundAddr Then Exit Do
        Loop
    End With
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
it is because you never change "Found" after your first Find, so it never changes to nothing, get rid of theActiveFoundCell and replace it with "Found"
 
Upvote 0
it is because you never change "Found" after your first Find, so it never changes to nothing, get rid of theActiveFoundCell and replace it with "Found"

So i changed the code, but now it stops after the first 'hit' in the search. 😵‍💫

VBA Code:
    Dim Found As Range
    Dim FoundRegel As Long
    Dim FirstFoundAddr As String
    Dim ActiveFoundCell As Range
    Set SearchRange = Range("A:AZ")
      
    With SearchRange
    Set Found = Cells.Find(What:="AAA", LookIn:=xlFormulas, LookAt:=xlPart)
    If Not Found Is Nothing Then
    FirstFoundAddr = Found.Address
    FoundRegel = Found.Row
    End If
        Do Until Found Is Nothing
            If PlanWb.Sheets(CurSheet).Range("P" & FoundRegel).Value = 1 Then
            PlanWb.Sheets(CurSheet).Range("R" & FoundRegel).Value = 10
            PlanWb.Sheets(CurSheet).Range("S" & FoundRegel).Value = "Checked"
            PlanWb.Sheets(CurSheet).Range("Z" & FoundRegel).Value = "0:15"
            End If
        Set Found = .FindNext(After:=Found)
            If Found.Address = FirstFoundAddr Then Exit Do
        Loop
    End With
 
Upvote 0
Alright, i fixed the looping error, but now when i moved to a different sheet (with different structure) and adjust the SearchRange it feels like the SearchRange is not actually being used for the Search.
Am i doing this right? Am i using the correct syntax to make the Find function search within the SearchRange?

VBA Code:
    Dim Found As Range
    Dim LZVRegel As Long
    Dim FirstFoundAddr As String
    Dim ActiveFoundCell As Range
    Set SearchRange = Range("AP:CD")

    
    With SearchRange
    Set Found = Cells.Find(What:="A1234", LookIn:=xlFormulas, LookAt:=xlPart)
    If Not Found Is Nothing Then
    FirstFoundAddr = Found.Address
    
        Do Until Found Is Nothing
            LZVRegel = Found.Row
            If RittenplanWb.Sheets(CurSheet).Range("BE" & LZVRegel).Value = 1 Then
            RittenplanWb.Sheets(CurSheet).Range("BG" & LZVRegel).Value = 10
            RittenplanWb.Sheets(CurSheet).Range("BH" & LZVRegel).Value = "Test"
            RittenplanWb.Sheets(CurSheet).Range("BO" & LZVRegel).Value = "0:30"
            End If
        Set Found = .FindNext(After:=Found)
            If Found.Address = FirstFoundAddr Then Exit Do
        Loop
    End If
    End With
 
Upvote 0
You need to put the period “.” before cells so that it uses the With statement
Set Found = .Cells.Find(What:="A1234", LookIn:=xlFormulas, LookAt:=xlPart)
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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