Error While Using FindNext

kingnutin

Board Regular
Joined
Jun 15, 2005
Messages
67
Using Excel 2007 in an xlsm format.

I am having trouble with find routine. I get the old "object variable or with block variable not set" error on my "Loop While Not formulaRng Is Nothing And formulaRng.Address <> firstaddress" section of the code.

If I comment out the code
Code:
Worksheets("SVG").Range("A" & r - 9 & ":" & "B" & r + 1).ClearContents
everything works fine.

I know that there are several duplicate instances of the search term in the selected range. It is as if it searches for all the instances, but after it comes to the last one and searches again it fails when I use the code above.

When the error occurs the variables have the following values:
formulaRng = Nothing
formulaRng.Address = Object variable or With block variable not set
firstAddress = "$A$39495"

Any ideas on how to clear out the contents of a range of cells and make this work? I tried setting there value to just "", but didn't have any luck.

Code:
Code:
Dim formulaRng As Range
Dim formula(1 To 7) As String
Dim firstFormula As Long
Dim firstAddress as String
dim j as integer
dim r as long

firstFormula = 99999
formula(1) = "(MAX(((IN8 - IN7))"
formula(2) = "((MAX(((IN7 - IN6))"
formula(3) = "((MAX(((IN6 - IN5))"
formula(4) = "((MAX(((IN5 - IN4))"
formula(5) = "((MAX(((IN4 - IN3))"
formula(6) = "((MAX(((IN3 - IN2))"
formula(7) = "((IN2 - IN1)))))))))))))))))))"

With Worksheets("SVG").Range("A1:A40000")
    For j = 1 To 7
        Set formulaRng = .Find(formula(j), LookIn:=xlValues)
        If Not formulaRng Is Nothing Then
            firstaddress = formulaRng.Address
            Do
                If formulaRng.Row < firstFormula Then
                    firstFormula = formulaRng.Row - 9
                End If
                r = formulaRng.Row
                'Clear contents of cells
                Worksheets("SVG").Range("A" & r - 9 & ":" & "B" & r + 1).ClearContents
                Set formulaRng = .FindNext(formulaRng)
            Loop While Not formulaRng Is Nothing And formulaRng.Address <> firstaddress
        End If
    Next j
End With

If firstFormula < 99999 Then
    Worksheets("Comments").Range("A1:B52").Copy Destination:=Worksheets("SVG").Range("A" & firstFormula)
End If
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
If you are clearing the contents it won't be found again at firstaddress so you just need:

Code:
Loop While Not formulaRng Is Nothing
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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