Solved

Insert multiple individual rows in an Excel spreadsheet

Posted on 2013-05-12
3
422 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

758 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