Solved

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

Posted on 2009-04-13
7
725 Views
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.

Thanks
ee-format-samp.xls
0
Comment
Question by:LeeStink
  • 3
7 Comments
 
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...
Saurabh...

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

        Else

            i = i + 1

        End If

    Loop
 

    MsgBox "Done"

    Application.ScreenUpdating = True

End Sub

Open in new window

0
 
LVL 50

Accepted Solution

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

'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

        Do

            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

0
 
LVL 1

Expert Comment

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

        Loop

    End With

    Range("A6").Select

End Sub

Open in new window

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 24408433
Any update here LeeStink?
Cheers
Dave
0
 
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

Regards

Dave
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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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 demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

895 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

13 Experts available now in Live!

Get 1:1 Help Now