Create a Macro with button to generate the next sequential number in an Alphanumeric string

mangotti

New Member
Joined
Apr 24, 2015
Messages
6
I looking for some assistance in creating a macro that when a button is clicked, the macro sequentially increases an alphanumeric string, example of the string is as follows and the number should increase by 1 after the macro is run.

CA-123456

When macro is run, next alphanumeric will be CA-123457

Thanks!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG27Apr46
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]If[/COLOR] InStr(Selection, "-") > 0 [COLOR="Navy"]Then[/COLOR]
    Sp = Split(Selection, "-")
    Selection = Sp(0) & "-" & Sp(1) + 1
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try:-
Code:
[COLOR=Navy]Sub[/COLOR] MG27Apr46
[COLOR=Navy]Dim[/COLOR] Sp [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]If[/COLOR] InStr(Selection, "-") > 0 [COLOR=Navy]Then[/COLOR]
    Sp = Split(Selection, "-")
    Selection = Sp(0) & "-" & Sp(1) + 1
[COLOR=Navy]End[/COLOR] [COLOR=Navy]If[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

Thanks Mick.
I have tried this but receive an error to debug this line

Code:
If InStr(Selection, "B4") > 0 Then

Below is my code. I assume the "-" is the cell reference and my number to increment is in cell B4

Code:
Sub MG27Apr46()
Dim Sp As Variant
If InStr(Selection, "B4") > 0 Then
    Sp = Split(Selection, "B4")
    Selection = Sp(0) & "B4" & Sp(1) + 1
End If
End Sub
 
Upvote 0
Assuming cell B4 is the cell with the number...
Code:
Sub AddOneToCellB4()
  [b4] = Application.Substitute([b4], Right([b4], 6), Right([b4], 6) + 1)
End Sub
 
Upvote 0
Try:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG27Apr51
[COLOR="Navy"]Dim[/COLOR] Sp [COLOR="Navy"]As[/COLOR] Variant
[COLOR="Navy"]With[/COLOR] Range("B4")
Sp = Split(.Value, "-")
    .Value = Sp(0) & "-" & Sp(1) + 1
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try:-
Code:
[COLOR=Navy]Sub[/COLOR] MG27Apr51
[COLOR=Navy]Dim[/COLOR] Sp [COLOR=Navy]As[/COLOR] Variant
[COLOR=Navy]With[/COLOR] Range("B4")
Sp = Split(.Value, "-")
    .Value = Sp(0) & "-" & Sp(1) + 1
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick

Mick,

Now I receive a runtime error "9": "Subset is out of range". It debugs this line:

Code:
Value = Sp(0) & "-" & Sp(1) + 1
 
Upvote 0
This only works for fields containing numeric only. My field is alphanumeric.
Did you try it? If the last six characters of your field are digits (like you showed in your original message), it will work.
 
Upvote 0

Forum statistics

Threads
1,215,635
Messages
6,125,946
Members
449,275
Latest member
jacob_mcbride

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