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 techguruplus telegram group Join Our WhatsApp Group techguruplus whatsapp group
Nazim Khan - Author Image

Nazim Khan (Author) 📞 +91 9536250020
[MBA in Finance]

Nazim Khan is an expert in Microsoft Excel. He teaches people how to use it better. He has been doing this for more than ten years. He is running this website (TechGuruPlus.com) and a YouTube channel called "Business Excel" since 2016. He shares useful tips from his own experiences to help others improve their Excel skills and careers.

Leave a Comment