Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Insert multiple individual rows in an Excel spreadsheet

Posted on 2013-05-12
3
Medium Priority
?
464 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 1200 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 800 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
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…

971 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