How to Update Sequence Number for Rows Automatically After Inserting or Deleting Rows in Excel

Sometimes we add sequence number in the first column for each row in a table, for example create a table for saving student info, and one student one row. If I insert a new student info or delete an existing student info, the sequence numbers for rows are cut off and not continuous anymore. If we want to make these numbers are still continuous automatically after inserting or deleting rows, do you have any tricks? Actually, there are some methods you can follow to update sequence number for rows automatically, this article will introduce you two methods to implement this.

Precondition:

See table below. In first column, there are sequence numbers for rows.

Update Sequence Number for Rows Automatically 1

If we insert a new row or delete an existing row from the table, obviously numbers are not continuous. So, we need to know the method to update sequence number automatically.

Method 1: Update Sequence Number for Rows Automatically by Formula


Step 1: You can see that except the header, sequence number for each row equals to row number minus 1. So, we can use =ROW()-1 to get sequence number here. In B2 enter the formula =ROW()-1. Then drag it down to fill other cells till A13.

Update Sequence Number for Rows Automatically 2

Please notice that if there is no header in table and sequence number is started in A1, you can just enter =ROW() here.

Step 2: Create table for this table. Select this table, click Insert tab in ribbon, click Table.

Update Sequence Number for Rows Automatically 3

Verify that Create Table dialog pops up. As table range is already selected, so $A$1:$C$13 is auto selected, and option ‘My table has headers’ is checked by default. Click OK.

Update Sequence Number for Rows Automatically 4

Verify that table is created and table style is updated. Now, after creating table, sequence number will be automatically updated.

Update Sequence Number for Rows Automatically 5

Step 3: Try to insert a new row between number 6 and 7.

Update Sequence Number for Rows Automatically 6

Verify that sequence number is updated automatically.

Step 4: Try to delete an existing record.

Update Sequence Number for Rows Automatically 7

Verify that sequence number is updated automatically.

Method 2: Update Sequence Number for Rows Automatically by VBA Code


Notice: If you want to only delete row from table and then automatically update sequence number, you can also edit VBA code to implement this.

Step 1: On current visible worksheet, right click on sheet name tab to load Sheet management menu. Select View Code, Microsoft Visual Basic for Applications window pops up.

Or you can enter Microsoft Visual Basic for Applications window via Developer->Visual Basic. You can also press Alt + F11 keys simultaneously to open it.

Step 2: In Microsoft Visual Basic for Applications window, enter below code:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim i As Integer

    i = 2

    Application.EnableEvents = False

    For i = 2 To 13

        Range("A" & i).Value = i - 1

    Next

    Range("A13").Value = ""

    Application.EnableEvents = True

End Sub

Comments:

  1. Sequence number is started in A2, so we can define i=2, and For i = 2 To 13 (13 is the last row number in table), Range(“A” & i).Value = i – 1.
  2. In Range(“A13”).Value = “”, A13 is the last cell in sequence number. You can change it.

Step 3: Try to delete an existing record.

Update Sequence Number for Rows Automatically 8

Verify that sequence numbers are still continuous after updating.

 

 

Related Posts

If Cell is This Value or That Value

IF function is frequently used in Excel worksheet to return you expect “true value” or “false value” based on the result of logical test. If you want to see if a cell is A or B, and if one of ...

If Value is Greater Than A Certain Value
If Value is Greater Than A Certain Value 1

IF function is frequently used in Excel worksheet to return you expect “true value” or “false value” based on the logical test result. If you want to see if a value in one cell is greater than a specific value, ...

If Cell is Not Blank
If Cell is Not Blank 6

IF function is frequently used in Excel worksheet to return you expect “true value” or “false value” based on the result of created logical test. If you want to see if a cell is blank or not, and leave some ...

VBA Macro For VLOOKUP From Another Sheet
vba macro for vlookup from another sheet1

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you how to use VBA code to vlookup data ...

If Cell is Blank
If Cell is Blank_1

IF function is frequently used in Excel worksheet to return you expect “true value” or “false value” based on the result of created logical test. If you want to see if a cell is blank or not, and leave some ...

If Cell Equals Certain Text String
If cell equals certain text_1

IF function is frequently used in Excel worksheet to return you expect “true value” or “false value” based on the result of created logical test. If you want to see if cell equals a certain text string like “Win”, you ...

If Cell Contains Either Text1 or Text2
If cell contains text1 or text2_1

IF function is frequently used in Excel worksheet to return “true value” or “false value” based on the logical test result. If you want to see if cell contains certain substring1 like “abc” or substring2 like “def”, and returns true ...

If Cell Contains Certain Text OR Equals Certain Text

IF cell equals certain text IF function is frequently used in Excel worksheet to return “true value” or “false value” based on the logical test result. If you want to test values to see if they equal certain text like ...

VLOOKUP From Another Sheet Not Working
vlookup from another sheet not working3

In the previous post, you should know that how to fix or remove the #N/A error when using VLOOKUP formula to lookup value from another sheet. And this post will show you reasons why your VLOOKUP formula is not working ...

If Cell Begins with One of Three Supplied Characters
If Cell Begins with One of Three Supplied Characters

If you want to test values to see if they begin with some given specific characters like “x”, ”y”, or “z”, you can create a formula with COUNTIF and SUM functions to return results. EXAMPLE You can see “TRUE” or ...

Sidebar