Increment alphanumeric sequence to next row by 1

PierreT

New Member
Joined
Nov 7, 2014
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Good day,

I've found a macro online that increments cells with numerical value by 1 and inserts it in the row below when you double click the cell.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lastrow As Long
lastrow = Cells(Cells.Rows.Count, "k").End(xlUp).Row + 1
If Not Intersect(Target, Range("k:k")) Is Nothing Then
If Target.Address = Cells(lastrow, "k").Address Then
Application.EnableEvents = False
Cancel = True
Cells(lastrow, "k") = Cells(lastrow - 1, "k") + 1
Application.EnableEvents = True
End If
End If
End Sub

Is there a way the code can be altered to include alphanumerical value?
The data in my cells are as follow: LI0000, LI0001, LI0002 etc. The current formula will not support it.

Any help would be much appreciated.

Thank you.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If it is always two alpha then 4 numbers this should work

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lastrow As Long
lastrow = Cells(Cells.Rows.Count, "k").End(xlUp).Row + 1
If Not Intersect(Target, Range("k:k")) Is Nothing Then
    If Target.Address = Cells(lastrow, "k").Address Then
        Application.EnableEvents = False
        Cancel = True
        firsttwo = Left(Cells(lastrow - 1, "K"), 2)
        nums = Right(Cells(lastrow - 1, "K"), 4)
        Cells(lastrow, "k") = firsttwo & Format(nums + 1, "0000")
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
Works like a charm. Thank you.

If it is not too much to ask, as the numbers gets inserted in the next row, how could I get a border around the cell K (which is the cell with the number in) and cell , L and M)?

Thanks again.
 
Upvote 0
Not sure of you want it around the range K to M or each cell in K to M so the code for both is included just uncomment the line you want. The other line can be deleted.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim lastrow As Long
lastrow = Cells(Cells.Rows.Count, "k").End(xlUp).Row + 1
If Not Intersect(Target, Range("k:k")) Is Nothing Then
    If Target.Address = Cells(lastrow, "k").Address Then
        Application.EnableEvents = False
        Cancel = True
        firsttwo = Left(Cells(lastrow - 1, "K"), 2)
        nums = Right(Cells(lastrow - 1, "K"), 4)
        Cells(lastrow, "k") = firsttwo & Format(nums + 1, "0000")
[INDENT]'Uncomment the line below (remove the ' at the start of the line) to make border around each cell[/INDENT]
        'Range("K" & lastrow & ":M" & lastrow).Borders.LineStyle = xlContinuous
[INDENT]'Uncomment the line below (remove the ' at the start of the line) to make border around the range[/INDENT]
        'Range("K" & lastrow & ":M" & lastrow).BorderAround (xlContinuous)
        Application.EnableEvents = True
    End If
End If
End Sub
 
Upvote 0
Thanks for your help Scott T. and thanks for the quick turn around.
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,592
Members
449,174
Latest member
chandan4057

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