legalhustler
Well-known Member
- Joined
- Jun 5, 2014
- Messages
- 1,214
- Office Version
- 365
- Platform
- 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