A VBA code to Insert new row if sequential numbering not matching

MagnusE

New Member
Joined
Dec 29, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hello

I'm trying to figure out a code that could insert a new row where the sequential numbering mismatching.

As you can see in the picture a row is missing for POS 2. I would like a code that could insert, in multiple places, during the sequence. For example, in some cases the sheet could be missing POS 3, 8, 15. And even the first POS1 (B2) could also be missing.
The amount of rows could be between 10 to 200

I have no example because I'm a newbie in this area.


1609250804463.png
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi MagnusE,

As per description, assumption is that POS will start from 1 and will be in a sequence.

Is the understanding correct ?

Thanks,
Saurabh
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub MagnusE()
   Dim i As Long
   
   For i = Range("B" & Rows.count).End(xlUp).Row To 2 Step -1
      If Cells(i, 2) <> Cells(i - 1, 2) + 1 Then
         Rows(i).Resize(Cells(i, 2) - Cells(i - 1, 2) - 1).Insert
      End If
   Next i
End Sub
 
Upvote 0
Hi & welcome to MrExcel.
How about
VBA Code:
Sub MagnusE()
   Dim i As Long
  
   For i = Range("B" & Rows.count).End(xlUp).Row To 2 Step -1
      If Cells(i, 2) <> Cells(i - 1, 2) + 1 Then
         Rows(i).Resize(Cells(i, 2) - Cells(i - 1, 2) - 1).Insert
      End If
   Next i
End Sub
Hello Fluff

I get a new row but I also get "Run Time error" 13
 
Upvote 0
Are the values in col B actual numbers, or text?
 
Upvote 0
Do you want the news rows to have the POS# in col B?
 
Upvote 0
Hi MagnusE,

Please use below code:


VBA Code:
Sub InsertmissingRows()
    Dim rowNo As Integer, pos As Integer, noOfRowsReqd As Integer
    pos = 1
    rowNo = 2   
    Application.ScreenUpdating = False
   
    With ActiveSheet
        Do While .Range("B" & Rows.Count).End(xlUp).Row >= rowNo
            If .Range("B" & rowNo) >= rowNo Then
                    noOfRowsReqd = .Range("B" & rowNo) - rowNo - 1
                    .Range(rowNo & ":" & rowNo).EntireRow.Insert
            End If
            rowNo = rowNo + 1
        Loop
    End With
   
   Application.ScreenUpdating = True
End Sub

Thanks,
Saurabh
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,148
Members
453,021
Latest member
Justyna P

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