Handle Cancellation on input box (Range)

SkywardPalm

Board Regular
Joined
Oct 23, 2021
Messages
61
Office Version
  1. 365
Platform
  1. Windows
I am having trouble handling the cancellation of an input box that pops up for this macro. Currently, if I press cancel it will continue Next instead of Exit Sub. I am not sure what the issue is but I assume it's due to the range input box.. The second input box cancels fine.

VBA Code:
Public Sub RangeInputBox()

Dim rg As Range

On Error Resume Next
Set rg = Application.Selection
Set rg = Application.InputBox(Title:="Merge Cells", Default:=rg.Address, _
    Prompt:="Select your range:", Type:=8)
Err.Clear

'to ensure User Did not cancel
'below line causes OK to Exit Sub instead of move Next
'If StrPtr(rg) = 0 Then Exit Sub
If rg Is Nothing Then Exit Sub
On Error GoTo 0

Separator = Application.InputBox("Separate values with:", Title:="Merge Cells", Default:=" ")
If Separator = False Then
    Debug.Print "User Canceled!"
    Exit Sub
End If
End Sub
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Instead of ignoring the error with a "Resume Next" statement, try using error handling instead, something like this:
VBA Code:
Public Sub RangeInputBox()

Dim rg As Range

On Error GoTo err_chk
Set rg = Application.Selection
Set rg = Application.InputBox(Title:="Merge Cells", Default:=rg.Address, _
    Prompt:="Select your range:", Type:=8)

'to ensure User Did not cancel
'below line causes OK to Exit Sub instead of move Next
'If StrPtr(rg) = 0 Then Exit Sub
If rg Is Nothing Then Exit Sub


Separator = Application.InputBox("Separate values with:", Title:="Merge Cells", Default:=" ")
If Separator = False Then
    Debug.Print "User Canceled!"
    Exit Sub
End If

Exit Sub


err_chk:
    If Err.Number = 424 Then
        MsgBox "Process cancelled", vbOKOnly
    Else
        MsgBox Err.Number & ":" & Err.Description, vbOKOnly
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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