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.
 
again:
VBA Code:
Option Explicit
Sub NewSerialNumber()
Dim lr1&, lr2&, i&, cell As Range, max&, cID&, ws1 As Worksheet, ws2 As Worksheet
Set ws1 = Worksheets("LOG")
Set ws2 = Worksheets("STORE")
    lr1 = ws1.Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = ws2.Cells(Rows.Count, "A").End(xlUp).Row
    For Each cell In ws1.Range("A1:A" & lr1) 'loop column A of LOG, to get max
        If cell Like "GR-*" And Right(cell.Value, 6) > max Then max = Right(cell.Value, 6)
    Next
        For Each cell In ws2.Range("A1:A" & lr2) ' then, 'loop column A of STORE, to get final max
            If cell Like "GR-*" And Right(cell.Value, 6) > max Then max = Right(cell.Value, 6)
        Next
    ' generate new ID into last row of STORE?
    ws2.Range("A" & lr2 + 1).Value = Format(max + 1, "GR-000000")
End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Forum statistics

Threads
1,214,999
Messages
6,122,645
Members
449,093
Latest member
Ahmad123098

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