FindNext gives 91 error

aussiegrl14

New Member
Joined
Jun 23, 2009
Messages
3
I'm trying to make a macro to look for an "x" in a certain column, indicating the information should be duplicated from a different line, and then to copy and paste that information from the original into the duplicate line.

I have several instances of these duplicates so I want it to finish copying and pasting one duplicate, then continue to the next one. Here is the code:

Rich (BB code):
With Range("F5:F500")
    Set f = .Find("x", LookIn:=xlValues, Lookat:=xlWhole)                   'Finds a line designated as a "child"
 
        firstaddress = f.Address
        Do
            f.Activate
            desc = ActiveCell.Offset(0, -2)
            With Range("D5:D500")
                Set d = .Find(desc, LookIn:=xlValues, Lookat:=xlWhole)      'Look for "parent" line with same description
                secondaddress = d.Address
                Do
                    d.Activate
                    If ActiveCell.Offset(0, 2) = "x" Then                   'Make sure the line is a "parent" before copying
                        Set d = .FindNext(d)                                'Copies info from column I to W
                    Else
                        ActiveCell.Offset(0, 6).Select
                        Range(ActiveCell, ActiveCell.Offset(0, 14)).Copy
                        Set d = .FindNext(d)
                    End If
                Loop While Not d Is Nothing And d.Address <> secondaddress
            End With
            With Range("D5:D500")
                Set e = .Find(desc, LookIn:=xlValues, Lookat:=xlWhole)      'Look for the "child" lines
                thirdaddress = e.Address
                Do
                    e.Activate
                    If ActiveCell.Offset(0, 2) <> "x" Then                   'Confirm the line is a "child" before pasting
                        Set e = .FindNext(e)
                    Else
                        ActiveCell.Offset(0, 6).Select
                        Range(ActiveCell, ActiveCell.Offset(0, 14)).PasteSpecial (xlPasteAll)
                        Set e = .FindNext(e)
                    End If
                Loop While Not e Is Nothing And e.Address <> thirdaddress   'Find all "child" lines
            End With
            Set f = .FindNext(f)
        Loop While Not f Is Nothing And f.Address <> firstaddress           'Find all entries in need of duplicating
End With

However every time I try to run it I get a Run-time error '91': "Object variable or With block variable not set". It hits the error on the bold line.

Can anyone see what I did wrong? I'd appreciate any help you can give me.

Thanks!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Try changing this line :-

Code:
Loop While Not f Is Nothing And f.Address <> firstaddress

to this :-

Code:
    If f Is Nothing Then Exit Do
Loop While f.Address <> firstaddress
 
Upvote 0
Thanks for the response. I tried what you suggested and stepped through the code to see how it reacted. I got to this line:

Code:
 If f Is Nothing Then Exit Do

And apparently the .FindNext returned Nothing so it exited instead of continuing the loop. Any other ideas?
 
Upvote 0
Ok I searched through other threads and found the problem on my own. The issue is that the .FindNext method repeats only the LAST .Find terms in the code, instead of the ones I wanted it to use. To solve I replaced this line:

Code:
 Set f = .FindNext(f)

With this one:

Code:
 Set f = .Find("x", After:=f)

It works now, thanks for the indirect assistance :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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