Insert rows between sequence gaps

Tseug

New Member
Joined
Oct 6, 2009
Messages
2
Hello! I can picture the collective minds on this forum shaking their heads thinking "asked and answered", but I cannot find an answer for my particular instance anywhere. My apologies if I'm overlooking the absolute obvious! :(

I have an Excel file showing sales data that spans columns A - S and possibly 175 rows (down) if all the data is present. This almost never happens and I have large gaps in the sequence. I'd like a way to automatically insert X number of blank rows so that the sequence in column A matches the default row sequence of Excel.

......A..............B
1....1...........Cust A
2....5...........Cust E
3....9...........Cust I
4...11...........Cust K

would be...

......A...............B
1.....1............Cust A
2
3
4
5.....5............Cust E
6
7
8
9.....9............Cust I
10
11...11...........Cust K


Any help would be greatly appreciated. Thank you in advance.
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi there, welcome to the board!

You can use this code. Note a couple of things though. It assumes you'll run it on the active sheet. It also assumes your data is in cols A & B (col A houses a number of the destination row, col B houses your values), the entire row will be moved, and most importantly that the last row with data in it (the row) is not past that actual row. For example your data shows the last row as row 11. If you have data below row 11 before starting your results will be skewed and you will lose data.

Code:
Sub ExpandDataPlease()
    Dim i As Long, iRow As Long
    With ActiveSheet
        For i = .Cells(.Rows.Count, 1).End(xlUp).Row To 1 Step -1
            .Rows(i).Cut .Rows(.Cells(i, 1).Value)
        Next i
    End With
End Sub

To install this code (apologies if you already know) follow these steps. This is a one-time code.
  1. Open the VBE (alt + F11)
  2. Insert a blank module (Alt + I, M)
  3. Paste the above code
  4. Place your cursor anywhere in the sub routine and press F5 to run it
  5. Close the VBE, check results

IMPORTANT: Save all work before running this (a good habit).

HTH
 
Upvote 0
try this

Code:
Sub insertSeqRows()
Dim LR As Long, i As Long, x As Long
LR = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' get last row
For i = LR To 2 Step -1
    x = Cells(i, "A").Value - Cells(i - 1, "A").Value
 
    If x <> 1 Then
        Rows(i).Resize(x - 1).EntireRow.Insert
    End If
    
Next i
End Sub
 
Upvote 0
Hello,
With both these Codes I get a error. I clicked the debug and it highlights lines for each of the codes.

x = Cells(i, "A").Value - Cells(i - 1, "A").Value
.Rows(i).Cut .Rows(.Cells(i, 1).Value)


These codes used to work for me before. Not sure what I am doing differently now


try this

Code:
Sub insertSeqRows()
Dim LR As Long, i As Long, x As Long
LR = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row ' get last row
For i = LR To 2 Step -1
    x = Cells(i, "A").Value - Cells(i - 1, "A").Value
 
    If x <> 1 Then
        Rows(i).Resize(x - 1).EntireRow.Insert
    End If
    
Next i
End Sub
 
Upvote 0
OK I am the idiot. There was other terms in rows far below.. hence it didn't work. Sorry. Can't delete the post

Hello,
With both these Codes I get a error. I clicked the debug and it highlights lines for each of the codes.

x = Cells(i, "A").Value - Cells(i - 1, "A").Value
.Rows(i).Cut .Rows(.Cells(i, 1).Value)


These codes used to work for me before. Not sure what I am doing differently now
 
Upvote 0

Forum statistics

Threads
1,215,110
Messages
6,123,138
Members
449,098
Latest member
Doanvanhieu

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