VBA - Generate Serial Number max in two sheets

elbazi

New Member
Joined
Jun 21, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi guys,

have serial numbers with data in row in two different sheets (from main sheet divided later to two different, according the data in row). can someone help with a code to generate serial number after checking what is the max in the whole workbook? i am currently using +1 on the last row with data, but obviously its not correct seeing that its not necessary the max.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Assuming your main sheet is Sheet1 and your data is in column A you could try:
=MAX(Sheet1!A1:A1000)+1
 
Upvote 0
Assuming your main sheet is Sheet1 and your data is in column A you could try:
=MAX(Sheet1!A1:A1000)+1

Hi,

The max can be either on sheet1 or on sheet2, and it varies.
also, forgot to mention, the serial number should be built of two letters and than 5 numbers = aa-00001, aa-00002, aa-00003

thanks in advance
 
Upvote 0
You can try something like the following:
=MAX(NUMBERVALUE(RIGHT(Sheet1!A1:A1000,4)),NUMBERVALUE(RIGHT(Sheet2!A1:A1000,4)))
 
Upvote 0
If you want the answer in the same serial format, give this a try:
=LET(ser,MAX(NUMBERVALUE(RIGHT(Sheet1!A1:A1000,4)),NUMBERVALUE(RIGHT(Sheet2!A1:A1000,4))),"aa-"&REPT(0,4-LEN(ser))&ser+1)
 
Upvote 0
If you want the answer in the same serial format, give this a try:
=LET(ser,MAX(NUMBERVALUE(RIGHT(Sheet1!A1:A1000,4)),NUMBERVALUE(RIGHT(Sheet2!A1:A1000,4))),"aa-"&REPT(0,4-LEN(ser))&ser+1)

Hi Candyman8019,
thank you for the prompt reply.
Sorry for the confusion, maybe I haven't made myself clear,

This should be a VBA sub called during another sub is activated.

I am currently using the below code, but obviously its just generating a serial number of +1 to the latest row.

VBA Code:
Sub NewSerialNumber()
Dim lastCell As Range

    With Worksheets("Store")
    Set lastCell = .Cells(Rows.Count, "A").End(xlUp)
         If lastCell.Value = "Item ID" Then
           lastCell(2, 1).Value = "GR-000001"
          Else
           lastCell(2, 1).Value = Left(lastCell, 7) & Format(Right(lastCell, 3) + 1, "00")
         End If
  End With
  
End Sub
 
Upvote 0
Assume working with 2 sheets: named sheet1 and sheet2 (adjust to your actual name)
VBA Code:
Option Explicit
Sub NewSerialNumber()
Dim lr1&, lr2&, max&, cID&, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ActiveSheet
    If ws1.Name = "Sheet1" Then ' adjust sheet1 & sheet2 to actual names
        Set ws2 = Worksheets("Sheet2")
    ElseIf ws1.Name = "Sheet2" Then
        Set ws2 = Worksheets("Sheet1")
    End If
    lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
    With WorksheetFunction
        If ws1.Range("A" & lr1).Value Like "GR-*" Then cID = .max(max, Right(ws1.Range("A" & lr1).Value, 6))
        max = cID ' search for current ID in active sheet, save into max
        If ws2.Range("A" & lr2).Value Like "GR-*" Then cID = .max(max, Right(ws2.Range("A" & lr2).Value, 6))
        max = cID ' search for current ID in 2nd sheet, save into max
        ws1.Range("A" & lr1 + 1).Value = Format(max + 1, "GR-000000")
    End With
End Sub
 
Upvote 0
Assume working with 2 sheets: named sheet1 and sheet2 (adjust to your actual name)
VBA Code:
Option Explicit
Sub NewSerialNumber()
Dim lr1&, lr2&, max&, cID&, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = ActiveSheet
    If ws1.Name = "Sheet1" Then ' adjust sheet1 & sheet2 to actual names
        Set ws2 = Worksheets("Sheet2")
    ElseIf ws1.Name = "Sheet2" Then
        Set ws2 = Worksheets("Sheet1")
    End If
    lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
    With WorksheetFunction
        If ws1.Range("A" & lr1).Value Like "GR-*" Then cID = .max(max, Right(ws1.Range("A" & lr1).Value, 6))
        max = cID ' search for current ID in active sheet, save into max
        If ws2.Range("A" & lr2).Value Like "GR-*" Then cID = .max(max, Right(ws2.Range("A" & lr2).Value, 6))
        max = cID ' search for current ID in 2nd sheet, save into max
        ws1.Range("A" & lr1 + 1).Value = Format(max + 1, "GR-000000")
    End With
End Sub
Hi bebo021999,

I am not searching in active sheet but with two unrelated ones. i revised the code to my needs - but, it is always generating serial of GR-00001, i see its resetting the max with the second if and not checking where the max is bigger


VBA Code:
Sub NewSerialNumber()
Dim lr1&, lr2&, max&, cID&, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("LOG")
Set ws2 = Worksheets("STORE")
    lr1 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    On Error Resume Next
    With WorksheetFunction
        If ws1.Range("A" & lr1).Value Like "GR-*" Then cID = .max(max, Right(ws1.Range("A" & lr2).Value, 6))
        max = cID ' search for current ID in LOG, save into max
        If ws2.Range("A" & lr2).Value Like "GR-*" Then cID = .max(max, Right(ws2.Range("A" & lr1).Value, 6))
        max = cID ' search for current ID in STORE, save into max
        ws2.Range("A" & lr1 + 1).Value = Format(max + 1, "GR-000000")
    End With
End Sub
 
Upvote 0
Try to set lr1 (last row 1) to ws1, and lr2 to ws2
VBA Code:
lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
 
Upvote 0
Hi again,

thank you guys for helping.

im looking to have a code that is finding the MAX in column A between both sheets. the code you posted is giving me Column A's last row value.

the worksheet meant to register items into the store, or log it into the archive. at times, items will be archived not in a chronological order, and so it can come to serial1, serial4, serial2, serial 6, serial3. following the code, it will now give back serial4 once again because it found serial3 to be recent. can you help with finding highest value in two sheets please ?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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