Insert multiple individual rows in an Excel spreadsheet

Posted on 2013-05-12
Last Modified: 2013-05-27

Is there a way to insert multiple rows in various locations throughout an  Excel (2010) spreadsheet and avoid having to do it manually one-at-a-time?

For example, suppose you have entries in some number of consecutive rows as shown in Fig. 1 and suppose you want to insert a blank row directly above each cell with the entry "insert row¿".
Fig. 1
One way to do that is to select those specific rows one-at-a-time while holding down the control key (Fig. 2) and then insert new rows.

Fig. 2
Doing this of course results in the desired outcome (i.e. a blank row above each marker) as shown in Fig. 3.

Fig. 3
Now suppose that you are working in a spreadsheet with > 40K rows and your "insert row¿" markers total > 5K. In other words, say there are so many insertions to be made that it is not practical or feasible to manually select every row with a marker. Is there a way to insert rows directly above each marker but do the insertions en masse?

One thing I tried is using the Find box to select every marker in the column (Fig. 4)

Fig. 4
followed by using the insert command and selecting Entire Row (Fig. 5).

Fig. 5
That works great for any row containing a marker in which neither the row directly above nor the row directly below contains a marker. However, in any places where two or more consecutive rows contain markers, the insertion resulted in that many blank rows as a group above the group of markers as shown in Fig. 6.

Fig. 6
Is there a way to get around that result and have the insertions occur directly above each individually marked row?

Question by:Steve_Brady
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Accepted Solution

Flyster earned 300 total points
ID: 39160327
Here is a macro that will add a blank row above every instance of "Insert Row" . It is set to work in column B.
Sub AddRows()
Dim LstRow, Ctr, Adj As Integer

LstRow = Range("B65536").End(xlUp).Row
Adj = 0
Application.ScreenUpdating = False

  For Ctr = 1 To LstRow
  Range("B" & Ctr).Select
    If ActiveCell.Value = "Insert Row" Then
      Adj = Adj + 1
  End If
  Next Ctr
  For Ctr = 1 To LstRow + Adj
  Range("B" & Ctr).Select
    If ActiveCell.Value = "Insert Row" Then
      Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
      Ctr = Ctr + 1
  End If
  Next Ctr
Application.ScreenUpdating = False

End Sub

Open in new window

LVL 17

Assisted Solution

andrewssd3 earned 200 total points
ID: 39163097
One way to avoid having two loops is the common trick of starting at the bottom and working up.  I treat the first row separately for performance, so we don't have to check each time through, but you could have an If statement in the loop to check.
Public Sub RunInsert()

    Call InsertBlankRows(2)

End Sub

Public Sub InsertBlankRows(ByVal markerCol As Long)

    Dim rngToCheck As Range
    Const cINSERT_FLAG As String = "insert row"
    Set rngToCheck = ActiveSheet.Columns(markerCol)
    Dim c As Range
    ' get the last used cell in the range
    Set c = rngToCheck.Cells(rngToCheck.Worksheet.Rows.Count, 1).End(xlUp)

    Do While c.Row > 1
        If StrComp(c.Value, cINSERT_FLAG, vbTextCompare) = 0 Then
            Set c = c.Offset(-2, 0)
            Set c = c.Offset(-1, 0)
        End If
    ' do the top row if required
    If StrComp(c.Value, cINSERT_FLAG, vbTextCompare) = 0 Then
    End If

End Sub

Open in new window


Author Closing Comment

ID: 39199807

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question