Increment number in parenthesis by 1

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,364
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
So far I've been able to put together the following VBA code for replacing a number within the parentheses by 1.

However, if the cell does not contain parentheses, how do I avoid that error. Also, is there a cleaner way to do this.

In the example below, row 3 would change to 11 and row 5, 45, while keeping the parentheses.

VBA Code:
Sub Test()
    Dim mystr As String
    Dim par1CharNum As Long
    Dim par2CharNum As Long
    Dim MyParseStr As Long
    Dim LastRow As Long: LastRow = Sheets("Birthdays").Cells(Rows.Count, "E").End(xlUp).Row
    Dim i As Long
    For i = 3 To LastRow
        mystr = Sheets("Birthdays").Range("E" & i).Value
        par1CharNum = InStr(1, mystr, "(")
        par2CharNum = InStr(par1CharNum, mystr, ")")
        MyParseStr = Mid(mystr, par1CharNum + 1, par2CharNum - par1CharNum - 1)
        Sheets("Birthdays").Range("E" & i).Value = Left(mystr, par1CharNum - 2) & " (" & MyParseStr + 1 & ")"
    Next i
End Sub

Calendar.xlsm
E
1Name
2Mickey Mouse
3Donald Duck (10)
4George Washington
5Abraham Lincoln (44)
Sheet2
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
My macro works for this case:


varios 24nov2022.xlsm
E
1Name
2Mickey Mouse
3Donald Duck (11)
4George Washington
5Abraham Lincoln (45)
6George Brett Sammy Sosa (35)
7Cal Ripkin Mickey Mantle (34) Babe Ruth Lionel Messi (34)
8Alfred Hitchcock (56) Clint Eastwood (19)
Birthdays
 
Upvote 0

Forum statistics

Threads
1,215,044
Messages
6,122,827
Members
449,096
Latest member
Erald

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