Having issues with .FindNext

lockarde

Board Regular
Joined
Oct 23, 2016
Messages
77
Hello all,

I have a macro that runs through a column looking for text - it successfully finds the first instance of the text, however, when I use the .FindNext, the range comes up empty, even though there are 7 more instances of the text. I've tried playing with the search parameters, re-entering the text to make sure the value in the cell is correct... I can't seem to get it to work! The idea here is I have monthly sheets tracking jobs, and I also have a calendar sheet that, once it completes "building" the calendar, searchs the monthly sheets for jobs that occurred during that month, then it puts job details on the calendar.
Item(s)
Item 1
Notes:
Notes:
Notes:
Notes:
Item(s)
Item 1
Notes:
Item(s)
Item 1
Notes:
Notes:
Notes:
Item(s)
Item 1
Notes:
Item(s)
Item 1
Notes:
Item(s)
Item 1
Notes:
Notes:
Item(s)
Item 1
Notes:
Item(s)
Item 1
Notes:

That's what the colum looks like, and my code is below:
VBA Code:
For x = 8 To shtcnt
    With Sheets(x)
        xshtname = Sheets(x).Name
        Set lastjob = Sheets(x).Cells.Find(what:="Notes:", searchorder:=xlByColumns, searchdirection:=xlPrevious)
        lastrow = lastjob.Row + 1
                
        Set jobfind = Sheets(x).Range("A1:A250").Find("Item 1", lookat:=xlWhole, LookIn:=xlFormulas)
        first_jf_Address = jobfind.Address
        
        Set c = Sheets(x).Cells.Find("Req Date")
        Set fc = Sheets(x).Cells.Find("Location")
        Set cs = Sheets(x).Cells.Find("Crate Style")
    
        Do
              
            duedate = .Cells(jobfind.Row, c.Column).Value
            francode = .Cells(jobfind.Row, fc.Column).Value
            crtstyle = .Cells(jobfind.Row, cs.Column).Value
            jobdet = francode & " - " & crtstyle
            duemos = Month(duedate)
            dueyr = Year(duedate)
            dueday = day(duedate)
            
            If duemos = i And dueyr = yr Then
                Set duerng = Sheets("Calendar").Cells.Find(dueday, lookat:=xlWhole)
                Set duerng = duerng.Offset(1, 0)
                If Not duerng.Value2 = "" Then
                duerng.Value2 = duerng.Value2 & Chr(10) & Chr(10) & jobdet
                Else
                duerng.Value2 = jobdet
                End If
                'r = Sheets(x).Cells.FindNext(what:="Item 1", searchorder:=xlByColumns, searchdirection:=xlNext).Row
                
            Else
                
            End If
        Set jobfind = Sheets(x).Range("A1:A250").FindNext(jobfind)
'this is where the issue occurs, after this line, jobfind = Empty
        next_jf_Address = jobfind.Address
        Loop While jobfind.Address <> first_jf_Address
    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.
Hi,
try modifying your code as follows as see if resolves your issue

VBA Code:
Set jobfind = Sheets(x).Range("A1:A250").Find("Item 1", lookat:=xlWhole, LookIn:=xlFormulas)
    If Not jobfinf Is Nothing Then
        first_jf_Address = jobfind.Address
        
        Do
' rest of your code





            
            Set jobfind = Sheets(x).Range("A1:A250").FindNext(jobfind)
            If jobfind Is Nothing Then Exit Do
            
        Loop While jobfind.Address <> first_jf_Address
        
    Else
        
        MsgBox "Record not Found", 48, "Not Found"
    End If

Dave
 
Upvote 0
FindNext will repeat the last Find, so these lines
VBA Code:
        Set c = Sheets(x).Cells.Find("Req Date")
        Set fc = Sheets(x).Cells.Find("Location")
        Set cs = Sheets(x).Cells.Find("Crate Style")
must go before
VBA Code:
 Set jobfind = Sheets(x).Range("A1:A250").Find("Item 1", lookat:=xlWhole, LookIn:=xlFormulas)
 
Upvote 0
FindNext will repeat the last Find, so these lines
VBA Code:
        Set c = Sheets(x).Cells.Find("Req Date")
        Set fc = Sheets(x).Cells.Find("Location")
        Set cs = Sheets(x).Cells.Find("Crate Style")
must go before
VBA Code:
Set jobfind = Sheets(x).Range("A1:A250").Find("Item 1", lookat:=xlWhole, LookIn:=xlFormulas)

Ahhh, that makes sense! How would you rectify it since I use .Find to search for the duedate in the Calendar sheet (middle of Do Loop)?

Simply re-adding the jobfind line would just make the code always find the second instance of "Item 1" correct? I'm having a brain fart on how to adequately keep track :/
 
Upvote 0
Missed that one :(
You will need to do a normal find, but start from the last found cell
 
Upvote 0
Missed that one :(
You will need to do a normal find, but start from the last found cell
So that'd look like?:
VBA Code:
Set jobfind = Sheets(x).Range("A1:A250").Find("Item1", lookat:=xlWhole, after:=jobfind.Address)
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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