Object variable or With block variable not set

brulet

New Member
Joined
May 6, 2011
Messages
20
Any idea why the error "Object variable or With block variable not set"? Works fine with a test routine, but not in a sub
IE

<code>
Sub testsmallest()
msgbox smallest(0.001, Worksheets("Site1.Frequency.1").Range("C1:C61")).Cells.Offset(0, -1)
End Sub
</code>

but not...

<code>
Function findNearest(referenceFlow As Double, findInMonth As Worksheet) As Variant
Dim larger, smaller As Range
Dim foundFlows As Variant


'larger = largest(referenceFlow, findInMonth.Range("G1:G17"))
smaller = smallest(referenceFlow, findInMonth.Range("G1:G17"))
</code>

<code>
Function smallest(ByVal searchValue As Double, searchRange As Range) As Range
Dim searchLocation As Variant
searchLocation = Application.CountIf(searchRange, ">=" & searchValue)
msgbox searchLocation
msgbox searchRange.End(xlDown).Address
msgbox searchRange.End(xlDown).Offset(-searchLocation, 0).Value


If IsError(searchLocation) = False Then
Set smallest = searchRange.End(xlDown).Offset(-searchLocation, 0)
msgbox TypeName(smallest)
Else
msgbox "Error 1 - Data set too small to generate table."
End If
End Function

</code>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This line:
Code:
smaller = smallest(referenceFlow, findInMonth.Range("G1:G17"))
should be:
Code:
Set smaller = smallest(referenceFlow, findInMonth.Range("G1:G17"))
 
Upvote 0
Since the Function smallest is returning a Range Object, then this line
smaller = smallest(referenceFlow, findInMonth.Range("G1:G17"))
should be
Set smaller = smallest(referenceFlow, findInMonth.Range("G1:G17"))


The reason it works in your test Sub, is because in the Test Sub it's not assigning the result of smallest to a variable object, it's just going straight to a message box.

Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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