Solved

Insert multiple individual rows in an Excel spreadsheet

Posted on 2013-05-12
3
433 Views
Last Modified: 2013-05-27
Hello,

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?

Thanks
0
Comment
Question by:Steve_Brady
3 Comments
 
LVL 22

Accepted Solution

by:
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

Flyster
0
 
LVL 17

Assisted Solution

by:andrewssd3
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
            c.EntireRow.Insert
            Set c = c.Offset(-2, 0)
        Else
            Set c = c.Offset(-1, 0)
        End If
    Loop
    
    ' do the top row if required
    If StrComp(c.Value, cINSERT_FLAG, vbTextCompare) = 0 Then
        c.EntireRow.Insert
    End If

End Sub

Open in new window

0
 

Author Closing Comment

by:Steve_Brady
ID: 39199807
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now