Macro for inserting a line

Ryyanks2

New Member
Joined
Jul 25, 2018
Messages
2
Hi,
I need help with making a macro in Excel. I Need it to insert a line below every 60 in the Duration column. I need this to happen for a chart size of A-K but be able to be done to mutliple charts without touching the ones it has already done! Thanks in advance for all the help!

NetworkPeriodIndicatorsLabel1TimeDays(000s)(000s)(000s)(000s)Duration
scooby7e4462168tugj30
scooby8e5462168tugj30
scooby9e6462168tugj30
scooby10e7462168tugj30
scooby11e8462168tugj30
scooby12e9462168tugj30
scooby13e10462168tugj60
scooby14e11462168tugj60
scooby15e12462168tugj60
scooby16e13462168tugj60
scooby17e14462168tugj60
scooby18e15462168tugj60
scooby19e16462168tugj60
scooby20e17462168tugj60
scooby21e18462168tugj60
scooby22e19462168tugj60
scooby23e20462168tugj60
scooby24e21462168tugj60
scooby25e22462168tugj60
scooby26e23462168tugj60
scooby27e24462168tugj60
scooby28e25462168tugj60
scooby29e26462168tugj60
scooby30e27462168tugj60
scooby31e28462168tugj60
scooby32e29462168tugj60
scooby33e30462168tugj60

<colgroup><col width="64" span="11" style="width:48pt"> </colgroup><tbody>
</tbody>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Give this macro a try...
Code:
Sub InsertRowBelowDuration60()
  Dim R As Long
  Application.ScreenUpdating = False
  For R = Cells(Rows.Count, "K").End(xlUp).Row To 2 Step -1
    If Cells(R, "K").Value = 60 Then Rows(R).Insert
  Next
  Application.ScreenUpdating = True
End Sub

HOW TO INSTALL MACROs
------------------------------------
If you are new to macros, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. To use the macro, go back to the worksheet with your data on it and press ALT+F8, select the macro name (InsertRowBelowDuration60) from the list that appears and click the Run button. The macro will execute and perform the action(s) you asked for. If you will need to do this again in this same workbook, and if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "Yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
Perhaps.
Code:
For I = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
    If Range("K" & I).Value = 60 Then
        Range("A" & I).Resize(, 11).Insert xlShiftDown
    End If
Next i
 
Upvote 0
Hi thanks for such a quick response! Is it possible to make it relative because the duration wont always be in K

Thanks!
 
Upvote 0
Hi thanks for such a quick response! Is it possible to make it relative because the duration wont always be in K
Give this modification to the code I posted in Message #2 a try...
Code:
[table="width: 500"]
[tr]
	[td]Sub InsertRowBelowDuration60()
  Dim R As Long, DurationCol As String
  DurationCol = Split(Rows(1).Find("Duration", , xlValues, xlWhole, , , False, , False).Address(1, 0), "$")(0)
  Application.ScreenUpdating = False
  For R = Cells(Rows.Count, DurationCol).End(xlUp).Row To 2 Step -1
    If Cells(R, DurationCol).Value = 60 Then Rows(R).Insert
  Next
  Application.ScreenUpdating = True
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
This assumes the headers are in row 1.
Code:
    Res = Application.Match("Duration", Rows(1), 0)

    If Not IsError(Res) Then 

        For I = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
            If Cells(I, Res).Value = 60 Then
                Range("A" & I).Resize(, 11).Insert xlShiftDown
            End If
        Next I

    End If
 
Upvote 0

Forum statistics

Threads
1,221,417
Messages
6,159,789
Members
451,589
Latest member
Harold14

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top