VBA Code to Add Serial Numbers in Excel

This VBA code defines a subroutine called “AddSerialNumbers.” Here is a summary of what the code does:

  1. The code declares a variable called “i” as an integer.
  2. It sets up an error handling mechanism with the line “On Error GoTo Last.” This means that if an error occurs during the execution of the code, it will jump to the “Last” label.
  3. It prompts the user with an input box asking for a value, which represents the total number of serial numbers to be added.
  4. The code then enters a loop that runs from 1 to the value entered by the user.
  5. Inside the loop, it assigns the current value of “i” to the value of the active cell in the worksheet.
  6. It moves the active cell one row down using the “Offset” method, so that the next serial number can be added to the cell below.
  7. The loop continues until the desired number of serial numbers have been added.
  8. If an error occurs during the loop, it jumps to the “Last” label, which then exits the subroutine using the “Exit Sub” statement.
  9. The subroutine ends.

In summary, this code allows the user to input a value representing the total number of serial numbers to be added. It then adds sequential serial numbers to the active worksheet, starting from the active cell and moving down to subsequent cells.

VBA Code:

Sub AddSerialNumbers()
Dim i As Integer
On Error GoTo Last
i = InputBox("Enter Value", "Enter Serial Numbers")
For i = 1 To i
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
Next i
Last:Exit Sub
End Sub

Check All VBA Codes

Join Our Telegram Group

Join the TechGuruPlus Telegram group

Join Our WhatsApp Group

Join the TechGuruPlus WhatsApp group
Nazim Khan, founder of TechGuruPlus.com

— Founder & Editor, TechGuruPlus.com
[MBA in Finance]

Nazim Khan has spent over ten years working with Excel, Tally and office documentation in accounts, finance and MIS roles. He has been running TechGuruPlus.com since 2016, where he publishes free editable templates for office work, and the YouTube channel Business Excel. He has trained more than 50,000 people online. Every template on this site is built and checked by him before it is published.

Contact  Â·  Excel Course

Leave a Comment