While this question is a little older, I still want to show the correct way to do this without errors. You can do this either using a function or using sub.
Your basic procedure looks something like this:
Sub test() Dim MyRange As Range testSub Application.InputBox("dada", , , , , , , 8), MyRange 'doing via Sub Set MyRange = testFunc(Application.InputBox("dada", , , , , , , 8)) ' doing via function If MyRange Is Nothing Then Debug.Print "The InputBox has been canceled." Else Debug.Print "The range " & MyRange.Address & " was selected." End If End Sub
Sub (funny):
Sub testSub(ByVal a As Variant, ByRef b As Range) If TypeOf a Is Range Then Set b = a End Sub
And the function will look like this:
Function testFunc(ByVal a As Variant) As Range If TypeOf a Is Range Then Set testFunc = a End Function
Now just use the method you like and delete the unused line.
If you call a sub or function, you do not need the Set parameter. However, it does not matter whether the InputBox returns an object or not. All you have to do is check if this parameter is the object you want or not, and then act accordingly.
EDIT
Another smart way is to do the same behavior with a collection like this:
Sub test() Dim MyRange As Range Dim MyCol As New Collection MyCol.Add Application.InputBox("dada", , , , , , , 8) If TypeOf MyCol(1) Is Range Then Set MyRange = MyCol(1) Set MyCol = New Collection If MyRange Is Nothing Then Debug.Print "The inputbox has been canceled" Else Debug.Print "the range " & MyRange.Address & " was selected" End If End Sub
If you still have questions, just ask;)