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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
457
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,645
Office Version
  1. 365
Platform
  1. Windows
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
 

MagnusE

New Member
Joined
Dec 29, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
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
Hello Saurabh

Yes this is correct!
 

MagnusE

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

ADVERTISEMENT

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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,645
Office Version
  1. 365
Platform
  1. Windows
Are the values in col B actual numbers, or text?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
63,645
Office Version
  1. 365
Platform
  1. Windows
Do you want the news rows to have the POS# in col B?
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
457
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
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
 
Solution

Forum statistics

Threads
1,143,677
Messages
5,720,259
Members
422,273
Latest member
linds75

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
Top