Object Required error in Excel Code

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Object Required error in Excel Code

Post by Rudi »

I am getting an error on this line in my code if I cancel the resulting inputbox. The error says: Object required?

How do I avoid this

Code: Select all

Set myHistogramTable = _
        Application.InputBox("Select a cell in the histogram table", _
                                    "Histogram Location", Type:=8)
        If Not myHistogramTable Is Nothing Then
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Object Required error in Excel Code

Post by HansV »

If the user cancels, InputBox returns the boolean value False, not a Range object.

You have to use On Error Resume Next:

Code: Select all

On Error Resume Next
Set myHistogramTable = _
  Application.InputBox("Select a cell in the histogram table", _
    "Histogram Location", Type:=8)
On Error Goto 0 ' or an error handler label
If Not myHistogramTable Is Nothing Then
Best wishes,
Hans

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: Object Required error in Excel Code

Post by Rudi »

Tx...it's working now!
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.