Incrementing last digit in a custom serial number

cherrydee

New Member
Joined
Jan 1, 2017
Messages
13
Hi, i am trying to increment the last digit of a custom field. An example is 2018-08-001. The code i am using is left(a1,10) & 1 + right(a1,1)
I am having issues when it reaches 2018-08-009. It becomes 0010. Also probable future problem is whenb it reaches 2018-08-999.
Any help is appreciated. Thank you!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Enter two first values manually
select yellowcells then drag down

2018-08-001
2018-08-002
2018-08-003
2018-08-004
2018-08-005
2018-08-006
2018-08-007
2018-08-008
2018-08-009
2018-08-010
2018-08-011
2018-08-012
2018-08-013
2018-08-014
2018-08-015

is that what you want?
 
Upvote 0
If you prefer a formula, this should get you to 2018-08-9999:
Excel Workbook
A
12018-08-001
22018-08-002
32018-08-003
42018-08-004
52018-08-005
62018-08-006
72018-08-007
82018-08-008
92018-08-009
102018-08-010
Test
 
Upvote 0
Hi, sorry if i wasn't clear. I'm doing this in vba so I can't simply drag down the formula.
Anyway, @joe, i am having an error in the "if section". Is that supposed to be another line of function?
 
Upvote 0
Hi, sorry if i wasn't clear. I'm doing this in vba so I can't simply drag down the formula.
Anyway, @joe, i am having an error in the "if section". Is that supposed to be another line of function?
Are you using the formula in VBA or directly on the worksheet? If in VBA, post the code and point to the line(s) where you use it. If on the worksheet, did you type it in or copy directly from your browser and paste it?
 
Upvote 0
I am using my phone to surf the web and i just copied your codes to a note app and typed it in my excel vba since i cant scroll horizontally on your codes.

Dim newvalue as string
Dim value as string
Value="2018-08-009"
Newvalue = left(value,8) & text(mid(value,9,len(value))+1,if(mid(value,9,len(value))+1<=999,"000","0000"))
 
Upvote 0
If you prefer a formula, this should get you to 2018-08-9999:

Spreadsheet Formulas
CellFormula
A2=LEFT(A1,8)&TEXT(MID(A1,9,LEN(A1))+1,IF(MID(A1,9,LEN(A1))+1<=999,"000","0000"))

<tbody>
</tbody>

<tbody>
</tbody>
I know the OP has since said he wants a VB solution although it is not clear how he wants it implemented (how do we know how many?), but I thought I would post a more compact formula for those who might like to see it...

=TEXT(1+SUBSTITUTE(A1,"-",""),"0000-00-000")

This will carry up to at least 999 and, if the next value is 000 rather than 001, then well beyond.
 
Upvote 0
I am using my phone to surf the web and i just copied your codes to a note app and typed it in my excel vba since i cant scroll horizontally on your codes.

Dim newvalue as string
Dim value as string
Value="2018-08-009"
Newvalue = left(value,8) & text(mid(value,9,len(value))+1,if(mid(value,9,len(value))+1<=999,"000","0000"))
You can't use the worksheet functions TEXT and IF like that in VBA. You would have to qualify them as worksheetfunction.Text(....) and double up the quotes on embedded strings.

Here's an easier way:
Code:
Sub IncrementByOne()
Dim StartValue As String, Increment As String, i As Long, V As Variant, NextValue As String
StartValue = "2018-08-001"
V = Split(StartValue, "-")
For i = 0 To 10000
    Increment = V(2) + i
    NextValue = Left(StartValue, 8) & Format(Increment, "000")
    'do something with NextValue. Here I write it to col A
    Range("A1").Offset(i).Value = NextValue
Next i
End Sub
 
Upvote 0
I know the OP has since said he wants a VB solution although it is not clear how he wants it implemented (how do we know how many?), but I thought I would post a more compact formula for those who might like to see it...

=TEXT(1+SUBSTITUTE(A1,"-",""),"0000-00-000")

This will carry up to at least 999 and, if the next value is 000 rather than 001, then well beyond.

Hi, your code works great! But how can i adjust it when it reaches thousands like 2018-08-1000? Cause I'm not really sure how long the records would be. Thanks!
Btw, i added worksheetfunction before substitute to make it work
 
Upvote 0
You can't use the worksheet functions TEXT and IF like that in VBA. You would have to qualify them as worksheetfunction.Text(....) and double up the quotes on embedded strings.

Here's an easier way:
Code:
Sub IncrementByOne()
Dim StartValue As String, Increment As String, i As Long, V As Variant, NextValue As String
StartValue = "2018-08-001"
V = Split(StartValue, "-")
For i = 0 To 10000
    Increment = V(2) + i
    NextValue = Left(StartValue, 8) & Format(Increment, "000")
    'do something with NextValue. Here I write it to col A
    Range("A1").Offset(i).Value = NextValue
Next i
End Sub

This works like a charm! :) Tho i removed the for loop since i put the nextvalue in msgbox and it won't stop. Lol. Anyway, not sure what the for loop is for.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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