Excel VBA:  dynamically insert blank rows after a row with a  call containing a ceritan value

Posted on 2009-04-13
Last Modified: 2012-05-06
Need to insert a blank row after the row containting "Non Target" (Column C).  The number of times the value "Non Target" cna appear will vary, so it should be dynamic.

Question by:LeeStink
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
  • 3
LVL 59

Assisted Solution

by:Saurabh Singh Teotia
Saurabh Singh Teotia earned 250 total points
ID: 24133143
Use this code and it will do what you are looking for...

Sub insertrows()
    Application.ScreenUpdating = False
    Dim i As Long
    i = 5
    Do Until i > Cells(65536, "C").End(xlUp).Row
        If Cells(i, "C").Value = "Non-Target" Then
            Rows(i + 1).Insert
            i = i + 2
            i = i + 1
        End If
    MsgBox "Done"
    Application.ScreenUpdating = True
End Sub

Open in new window

LVL 50

Accepted Solution

Dave Brett earned 250 total points
ID: 24134040
Using Find (as below) or Filter will offer the quickest code fix
Instructions to run thge macro included in the code below

'Press Alt + F11 to open the Visual Basic Editor (VBE)
'From the Menu, choose Insert-Module.
'Paste the code into the right-hand code window.
'Press Alt + F11 to close the VBE
'Go to Tools & Macro ... Macros and double-click InsRow
Sub InsRow()
    Dim rng1 As Range, rng2 As Range, cel As Range
    Dim FirstAddress As String
    Dim AppCalc As Long
    Application.ScreenUpdating = False
    Set cel = Columns("C").Find("Non-Target", , xlValues, xlWhole, xlByRows, , False)
    If Not cel Is Nothing Then
        Set rng2 = cel
        FirstAddress = cel.Address
            Set cel = Columns("C").FindNext(cel)
            Set rng2 = Union(rng2, cel)
        Loop While FirstAddress <> cel.Address
    End If
    If Not rng2 Is Nothing Then rng2.EntireRow.Offset(1, 0).Insert
    Application.ScreenUpdating = True
End Sub

Open in new window


Expert Comment

ID: 24340402
Hi LeeStink,

Try this code.
Sub InsertBlankRow()
    i = 6
    With ActiveSheet
        Do While .Cells(i, 3) <> ""
            If .Cells(i, 3) = "Non-Target" Then
                Rows(i + 1 & ":" & i + 1).Insert Shift:=xlDown
                i = i + 1
            End If
            i = i + 1
    End With
End Sub

Open in new window

LVL 50

Expert Comment

by:Dave Brett
ID: 24408433
Any update here LeeStink?
LVL 50

Expert Comment

by:Dave Brett
ID: 25875718
Recommend a split between http:#a24134040 and http:#a24133143 to recognise the first solution provided by Saurabh and the different approach by me which will cut run time



Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
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 Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

688 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