code for universal serial number in excel vba userform

Joined
Sep 16, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hi-
I have 6 department which I select from combobox1
1633226607931.png

If I select department as Press shop the present code gives serial no as PS 000001 ,if Machine shop then MS 000002 where both codes are separate
1633226839734.png

My present code is as follows which I copied from one website
VBA Code:
Sub serialno_MS()
Dim ws As Worksheet
    Dim lastSerial, digits, i As Integer
    Dim nextRow, lastrow As Long
    Dim newSerial As String
 Set ws = ThisWorkbook.Sheets("DATA")
nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1 'Finds the last row in A
    lastrow = nextRow - 1
    newSerial = "" 'set value of our string blank
If (nextRow - 1) < 2 Then 'If statement to catch if there's only a header
        lastSerial = 0
    Else: lastSerial = CInt(Replace(ws.Range("A" & lastrow).Value, "MS", ""))
    End If

    lastSerial = lastSerial + 1
    digits = 6 - Len(lastSerial) 'how many 0's are there

    For i = 1 To digits
        newSerial = newSerial & "0" 'start building the string with 0's
    Next i

    newSerial = "MS " & newSerial & lastSerial 'concatenate the serial code
    ws.Range("A" & nextRow).Value = newSerial
End Sub
I am looking for universal code if I take entry for Machine shop it should take last number of Machine shop entry and continue ie.MS 000001,if selected as Press shop it should take last entry number of Press shop and continue i.e "PS 000001"

can any one help?
 
Last edited by a moderator:

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When posting vba code please use the available code tags to implement/preserve formatting. My signature block below has more details. I have added the tags for you this time. :)
 
Upvote 0
Hi-
I have 6 department which I select from combobox1
1633226607931.png


If I select department as Press shop the present code gives serial no as PS 000001 ,if Machine shop then MS 000002 where both codes are separate
1633226839734.png


My present code is as follows which I copied from one website

VBA Code:
Sub serialno_MS()
Dim ws As Worksheet
Dim lastSerial, digits, i As Integer
Dim nextRow, lastrow As Long
Dim newSerial As String
Set ws = ThisWorkbook.Sheets("DATA")
nextRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1 'Finds the last row in A
lastrow = nextRow - 1
newSerial = "" 'set value of our string blank
If (nextRow - 1) < 2 Then 'If statement to catch if there's only a header
lastSerial = 0
Else: lastSerial = CInt(Replace(ws.Range("A" & lastrow).Value, "MS", ""))
End If

lastSerial = lastSerial + 1
digits = 6 - Len(lastSerial) 'how many 0's are there

For i = 1 To digits
newSerial = newSerial & "0" 'start building the string with 0's
Next i

newSerial = "MS " & newSerial & lastSerial 'concatenate the serial code
ws.Range("A" & nextRow).Value = newSerial
End Sub
I am looking for universal code if I take entry for Machine shop it should take last number of Machine shop entry and continue ie.MS 000001,if selected as Press shop it should take last entry number of Press shop and continue i.e "PS 000001"

can any one help?
 
Last edited by a moderator:
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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