Modify VBA Code To Do Nothing When Clicking Cancel

legalhustler

Well-known Member
Joined
Jun 5, 2014
Messages
1,214
Office Version
  1. 365
Platform
  1. Windows
The following code auto numbers a selected range that has merged cells. An input dialog pops that asks for to select/provide the range to auto number with the option to click "OK" or "Cancel", when I click "Cancel" it puts a "1" in a pre-selected cell. I would like to modify the code so that when I click cancel it should do nothing (not put a "1"). Can someone help. Thanks!


Code:
Sub NumberCellsAndMergedCells()
    Dim Rng As Range
    Dim WorkRng As Range
    Dim Str As String
    On Error Resume Next
    xTitleId = "Auto Numbering"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select Range:", xTitleId, WorkRng.Address, Type:=8)
    Set WorkRng = WorkRng.Columns(1)
    xIndex = 1
    Set Rng = WorkRng.Range("A1")
    Do While Not Intersect(Rng, WorkRng) Is Nothing
        Rng.Value = xIndex
        xIndex = xIndex + 1
        Set Rng = Rng.MergeArea.Offset(1)
    Loop
   End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Check if WorkRng is Nothing, and quit procedure (or do something else you need) if it is Nothing. If Cancel is clicked then it means object variable has not been set.
Sample:

Code:
...
Set WorkRng = Application.InputBox("Select Range:", xTitleId, WorkRng.Address, Type:=8)
If WorkRng Is Nothing Then
   Exit Sub
End If
...
 
Last edited:
Upvote 0
Check if WorkRng is Nothing, and quit procedure (or do something else you need) if it is Nothing. If Cancel is clicked then it means object variable has not been set.
Sample:

Code:
...
Set WorkRng = Application.InputBox("Select Range:", xTitleId, WorkRng.Address, Type:=8)
If WorkRng Is Nothing Then
   Exit Sub
End If
...

I only have rudimentary VBA experience. I'm not sure where in my code that needs to go. Please provide entire code.
 
Upvote 0
I only have rudimentary VBA experience. I'm not sure where in my code that needs to go. Please provide entire code.
If you look at his code, the first row matches EXACTLY one of your lines of code.
Just place the three new lines of code he shows under that one, keeping everything else exactly as it is (you are just inserting three new lines of code).
 
Last edited:
Upvote 0
If you look at his code, the first row matches EXACTLY one of your lines of code.
Just place the three new lines of code he shows under that one, keeping everything else exactly as it is (you are just inserting three new lines of code).

Here is the entire code with the new lines, however when I click the Cancel button it still puts a "1" in a cell.

Code:
Sub NumberCellsAndMergedCells()
    Dim Rng As Range
    Dim WorkRng As Range
    Dim Str As String
    On Error Resume Next
    xTitleId = "Auto Numbering"
    Set WorkRng = Application.Selection
    Set WorkRng = Application.InputBox("Select Range:", xTitleId, WorkRng.Address, Type:=8)
    [B]If WorkRng Is Nothing Then
    Exit Sub
    End If[/B]
    Set WorkRng = WorkRng.Columns(1)
    xIndex = 1
    Set Rng = WorkRng.Range("A1")
    Do While Not Intersect(Rng, WorkRng) Is Nothing
        Rng.Value = xIndex
        xIndex = xIndex + 1
        Set Rng = Rng.MergeArea.Offset(1)
    Loop
End Sub
 
Last edited:
Upvote 0
Remove following line. It is presetting WorkRng object variable, and verification fails.

Code:
 Set WorkRng = Application.Selection
 
Upvote 0
And you also have to remove the WorkRng reference from the InputBox. Final code should be:

Code:
Sub NumberCellsAndMergedCells()
    Dim Rng As Range
    Dim WorkRng As Range
    Dim Str As String
    On Error Resume Next
    xTitleId = "Auto Numbering"
    Set WorkRng = Application.InputBox("Select Range:", xTitleId, Type:=8)
    If WorkRng Is Nothing Then
        Exit Sub
    End If
    Set WorkRng = WorkRng.Columns(1)
    xIndex = 1
    Set Rng = WorkRng.Range("A1")
    Do While Not Intersect(Rng, WorkRng) Is Nothing
        Rng.Value = xIndex
        xIndex = xIndex + 1
        Set Rng = Rng.MergeArea.Offset(1)
    Loop
End Sub
 
Upvote 0
And you also have to remove the WorkRng reference from the InputBox. Final code should be:

Code:
Sub NumberCellsAndMergedCells()
    Dim Rng As Range
    Dim WorkRng As Range
    Dim Str As String
    On Error Resume Next
    xTitleId = "Auto Numbering"
    Set WorkRng = Application.InputBox("Select Range:", xTitleId, Type:=8)
    If WorkRng Is Nothing Then
        Exit Sub
    End If
    Set WorkRng = WorkRng.Columns(1)
    xIndex = 1
    Set Rng = WorkRng.Range("A1")
    Do While Not Intersect(Rng, WorkRng) Is Nothing
        Rng.Value = xIndex
        xIndex = xIndex + 1
        Set Rng = Rng.MergeArea.Offset(1)
    Loop
End Sub

Bingo! It appears to work now. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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