Instant error with application.inputbox cancel!!

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

Instant error with application.inputbox cancel!!

Post by Rudi »

Hi,

I am getting an error when I cancel the application.inputbox. I thought I had the right checks in place to handle the cancel of this object, but it debugs immediately even though I have on error resume next... What am I doing wrong??? TIA

Code: Select all

EH:
    MsgBox "The search statement... " & vbNewLine & vbNewLine & _
        "'The following portfolios were included in this compliance procedure'" & vbNewLine & vbNewLine & _
        " ...could not be found in this report. This statement helps to determine the location of" & _
        " the portfolio lists that need to be sorted. " & vbNewLine & vbNewLine & "Please supply " & _
        "the location manually...", vbExclamation
    On Error Resume Next
    'if user cancels input it produces error 424: Object required????
    Set PortfolioListLocation = Application.InputBox("Please select the first portfolio code.", _
        "Portfolio List Location", Range("A6").Address, Type:=8)
    On Error GoTo 0
    If PortfolioListLocation Is Nothing Then Exit Sub
    If PortfolioListLocation.Offset(-1, 0) <> "" Then PortfolioListLocation.EntireRow.Insert
    Set PortfolioListLocation = PortfolioListLocation.CurrentRegion.Cells(1).Offset(-2, 0)

Regards,
Rudi

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

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

Re: Instant error with application.inputbox cancel!!

Post by HansV »

In the Visual Basic Editor, select Tools | Options...
Activate the General tab.
In the Error Trapping section, make sure that "Break on All Errors" is NOT selected. You should either have "Break on Unhandled Errors", or (preferably) "Break in Class Module".
Best wishes,
Hans

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

Re: Instant error with application.inputbox cancel!!

Post by Rudi »

Hi Hans,

TX for the reply. It is already on Unhanded Errors?

Any other ideas?
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

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

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

Re: Instant error with application.inputbox cancel!!

Post by HansV »

In that case, I'd have to see (a copy of) the workbook. Your code works without problems for me.
Best wishes,
Hans

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

Re: Instant error with application.inputbox cancel!!

Post by Rudi »

TX....Here is the file.
You do not have the required permissions to view the files attached to this post.
Regards,
Rudi

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

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

Re: Instant error with application.inputbox cancel!!

Post by HansV »

You can't use On Error Resume Next in an error handling section. To work around this, use code like this:

Code: Select all

Private Sub FindPortfolioList()
    On Error GoTo EH
    Set PortfolioListLocation = Cells.Find(What:= _
        "The following portfolios were included in this compliance procedure", After _
        :=Range("A1"), LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows _
        , SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If PortfolioListLocation.Offset(1, 0) <> "" Then PortfolioListLocation.Offset(1, 0).EntireRow.Insert
    Exit Sub
ExitHandler:
    On Error Resume Next
    Set PortfolioListLocation = Application.InputBox("Please select the first portfolio code.", _
        "Portfolio List Location", Range("A6").Address, Type:=8)
    On Error GoTo 0
    If PortfolioListLocation Is Nothing Then Exit Sub
    If PortfolioListLocation.Offset(-1, 0) <> "" Then PortfolioListLocation.EntireRow.Insert
    Set PortfolioListLocation = PortfolioListLocation.CurrentRegion.Cells(1).Offset(-2, 0)
    Exit Sub
EH:
    MsgBox "The search statement... " & vbNewLine & vbNewLine & _
        "'The following portfolios were included in this compliance procedure'" & vbNewLine & vbNewLine & _
        " ...could not be found in this report. This statement helps to determine the location of" & _
        " the portfolio lists that need to be sorted. " & vbNewLine & vbNewLine & "Please supply " & _
        "the location manually...", vbExclamation
    Resume ExitHandler
End Sub
Best wishes,
Hans

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

Re: Instant error with application.inputbox cancel!!

Post by Rudi »

Thanks, its working on my side too. Your advice is very interesting!?

What determines the error handling section then, as you are also using a label to branch off code? Maybe the only way to "exit" a error handler is to "Resume" code? This can be a little confusing then??

BTW: I sort of picked up on this conundrum (running an error in an error) earlier and I tried to fix it by the statement on error goto 0. In other words:

EH:
on error goto 0
blah blah blah...
on error resume next
....

i thought that the on error goto 0 would switch off the error handling so it was not aware it was in a "error handler".

obviously, it did not work...

Thanks a stack for your help. I really appreciate it! Cheers
Regards,
Rudi

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

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

Re: Instant error with application.inputbox cancel!!

Post by HansV »

The line On Error GoTo 0 does not help because the error has already occurred by then. The line On Error Resume Next has no effect while you are handling an error, that's why

Set PortfolioListLocation = Application.InputBox(...)

displays an error message if the input box is canceled. But the line Resume ExitHandler ends the error handling, so you can then use On Error Resume Next again.
Best wishes,
Hans

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

Re: Instant error with application.inputbox cancel!!

Post by Rudi »

TX.

I've written a lot of error handling code down the line, but it's the first time I am really concious of the fact that a resume statement ends an error handler. It makes sense now...but what a learning curve to discover this??!!
Regards,
Rudi

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