Solved

Insert multiple individual rows in an Excel spreadsheet

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

770 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