Application.InputBox error trapping

R

Rafi

I have the following inputbox and need to trap erros

Set Rng= Application.InputBox(Msg1, Title1, Type:=8) where Rng is dimmed as
an object

1) how do I trap the user response if they click cancel
2) how do i tarp the user response if the click Enter without selecting a
range
 
E

egun

Does this help?

Sub TestIt()
Dim Rng As Object
'
On Error GoTo Bad_Rng
Set Rng = Application.InputBox(Msg1, Title1, Type:=8)
On Error GoTo 0
'
MsgBox "Got to here"
Exit Sub
'
Bad_Rng:
MsgBox "Cancel button was pressed"
End Sub

If the user clicks enter and the box is blank, or has a badly formed range
in it, then the InputBox Method itself displays a warning message and doesn't
let the user get away with it. The only two ways for the user to get out are
to enter a valid range, or to press Cancel. You can trap Cancel by doing
what I did above.

Eric
 
B

Barb Reinhardt

I'd probably do it this way

Sub TestIt()
Dim Rng As Object
'
Set Rng = Nothing

On Error Resume Next
Set Rng = Application.InputBox(Msg1, Title1, Type:=8)
On Error GoTo 0
'
If Rng Is Nothing Then
MsgBox ("No range was selected.")
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top