Sequencing Numbers Not Working

rodl66

New Member
Joined
Mar 8, 2023
Messages
26
Office Version
  1. 365
Platform
  1. Windows
I have an Excel 365 worksheet that has a button I want to use to run a macro. When clicked, I want the VBA code to look for column A of the first empty row and generate an MPF Number in the format “MPF-yyyy-####” where yyyy is the current year and “####” starts with “0001”. Each time the button is clicked, a new MPF Number that increases by 1 is entered into the next empty row, column A. When a new year starts, the number should reset at 0001 using the same format as before. The code I am trying follows; however, it keeps inputting MPF-2023-0001 each time instead of incrementing it to 0002, 0003, etc. Thank you in advance for your assistance.

VBA Code:
Sub GenerateMPFNumber()
    Dim lastRow As Long, mpfNumber As String, year As String, number As String
   
    ’ Get the last used row in column A
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
   
    ’ Check if the year has changed
    If Format(Date, “yyyy”) <> Range(“A” & lastRow - 1).Value Then
        ’ Reset number to 1 if a new year starts
        number = “0001”
    Else
        ’ Increment the existing number by 1
        number = Format(CInt(Right(Range(“A” & lastRow - 1).Value, 4)) + 1, “0000”)
    End If
   
    ’ Get the current year
    year = Format(Date, “yyyy”)
   
    ’ Generate the MPF number
    mpfNumber = “MPF-” & year & “-” & number
   
    ’ Enter the MPF number in the next empty row, column A
    Range(“A” & lastRow).Value = mpfNumber
End Sub
 
Last edited by a moderator:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this:

VBA Code:
Sub GenerateMPFNumber()
  Dim lastRow As Long, mpfNumber As String, nYear As String, number As String
  
  ' Get the last used row in column A
  lastRow = Cells(Rows.Count, 1).End(xlUp).Row
  
  ' Get the year from the previous line
  If InStr(1, Range("A" & lastRow).Value, "-") > 0 Then
    nYear = Split(Range("A" & lastRow).Value, " - ")(1)
  End If
  
  ' Check if the year has changed
  If Format(Date, "yyyy") <> nYear Then
    number = "0001"       ' Reset number to 1 if a new year starts
  Else
    ' Increment the existing number by 1
    number = Format(CInt(Right(Range("A" & lastRow).Value, 4)) + 1, "0000")
  End If
  
  ' Enter the MPF number in the next empty row, column A
  Range("A" & lastRow + 1).Value = "MPF - " & Format(Date, "yyyy") & " - " & number
End Sub

Regards
Dante Amor
:giggle:
 
Upvote 1
Solution
Try changing this
VBA Code:
    If Format(Date, "yyyy") <> Range("A" & lastRow - 1).Value Then
to this
VBA Code:
    If Format(Date, "yyyy") <> Mid(Range("A" & lastRow - 1).Value, 7, 4) Then
 
Upvote 0
Try this:

VBA Code:
Sub GenerateMPFNumber()
  Dim lastRow As Long, mpfNumber As String, nYear As String, number As String
 
  ' Get the last used row in column A
  lastRow = Cells(Rows.Count, 1).End(xlUp).Row
 
  ' Get the year from the previous line
  If InStr(1, Range("A" & lastRow).Value, "-") > 0 Then
    nYear = Split(Range("A" & lastRow).Value, " - ")(1)
  End If
 
  ' Check if the year has changed
  If Format(Date, "yyyy") <> nYear Then
    number = "0001"       ' Reset number to 1 if a new year starts
  Else
    ' Increment the existing number by 1
    number = Format(CInt(Right(Range("A" & lastRow).Value, 4)) + 1, "0000")
  End If
 
  ' Enter the MPF number in the next empty row, column A
  Range("A" & lastRow + 1).Value = "MPF - " & Format(Date, "yyyy") & " - " & number
End Sub

Regards
Dante Amor
:giggle:
Sir,

You are a gentleman and a scholar, this worked. Thank you kindly.
 
Upvote 0
Im glad to help you.
Thank you for the feedback and your kind words.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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