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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Enter two first values manually
select yellowcells then drag down

[Table="width:, class:head"]
[tr=bgcolor:#FFFFFF][td=bgcolor:#FFFF00]2018-08-001[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#FFFF00]2018-08-002[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-003[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-004[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-005[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-006[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-007[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-008[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-009[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-010[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-011[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-012[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-013[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-014[/td][/tr]

[tr=bgcolor:#FFFFFF][td]2018-08-015[/td][/tr]
[/table]


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,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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