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!

[TABLE="width: 704"]
<colgroup><col width="64" span="11" style="width:48pt"> </colgroup><tbody>[TR]
[TD="class: xl71, width: 64"]Network[/TD]
[TD="class: xl71, width: 64"]Period[/TD]
[TD="class: xl71, width: 64"]Indicators[/TD]
[TD="class: xl71, width: 64"]Label1[/TD]
[TD="class: xl71, width: 64"]Time[/TD]
[TD="class: xl71, width: 64"]Days[/TD]
[TD="class: xl71, width: 64"](000s)[/TD]
[TD="class: xl71, width: 64"](000s)[/TD]
[TD="class: xl71, width: 64"](000s)[/TD]
[TD="class: xl71, width: 64"](000s)[/TD]
[TD="class: xl71, width: 64"]Duration[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]7[/TD]
[TD]e4[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]8[/TD]
[TD]e5[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]9[/TD]
[TD]e6[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]10[/TD]
[TD]e7[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]11[/TD]
[TD]e8[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]12[/TD]
[TD]e9[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]30[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]13[/TD]
[TD]e10[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]14[/TD]
[TD]e11[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]15[/TD]
[TD]e12[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]16[/TD]
[TD]e13[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]17[/TD]
[TD]e14[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]18[/TD]
[TD]e15[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]19[/TD]
[TD]e16[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]20[/TD]
[TD]e17[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]21[/TD]
[TD]e18[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]22[/TD]
[TD]e19[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]23[/TD]
[TD]e20[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]24[/TD]
[TD]e21[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]25[/TD]
[TD]e22[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]26[/TD]
[TD]e23[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]27[/TD]
[TD]e24[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]28[/TD]
[TD]e25[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]29[/TD]
[TD]e26[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]30[/TD]
[TD]e27[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]31[/TD]
[TD]e28[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]32[/TD]
[TD]e29[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
[TR]
[TD]scooby[/TD]
[TD="align: right"]33[/TD]
[TD]e30[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]8[/TD]
[TD]tugj[/TD]
[TD="align: right"]60[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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