Insert new row below and copy format

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
Can anyone help me with a small piece of code?

Here is what I need to do.

A user will select a cell or range of cells. I would like a macro which when run would insert a new row below the slected cell(s) and copy the format of the row above the new one into the new row.

Thanks
David
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

onlyadrafter

Well-known Member
Joined
Aug 19, 2003
Messages
5,703
Platform
  1. Windows
Hello,

I Know select is bad, I just cant think of a way round it at the moment.

Try this - untested really!

Code:
Sub copy_from_above()
ActiveCell.Offset(Selection.Rows.Count, 0).Select
Selection.EntireRow.Insert
Rows(ActiveCell.Row).Offset(-1, 0).Copy
Range("A" & ActiveCell.Row).PasteSpecial (xlPasteFormats)
Application.CutCopyMode = False
End Sub

I dont think you really need these lines as it seems to copy the line above anyway

Code:
Rows(ActiveCell.Row).Offset(-1, 0).Copy
Range("A" & ActiveCell.Row).PasteSpecial (xlPasteFormats)


Actually try

Code:
Sub copy_from_above()
ActiveCell.Offset(Selection.Rows.Count, 0).EntireRow.Insert
End Sub

or even

Code:
Sub copy_from_above()
ActiveCell.Offset(Selection.Rows.Count, 0).EntireRow.Insert
Rows(ActiveCell.Row).Offset(Selection.Rows.Count - 1, 0).Copy
Range("A" & ActiveCell.Offset(Selection.Rows.Count, 0).Row).PasteSpecial (xlPasteFormats)
Application.CutCopyMode = False
End Sub

again I don't think you need these

Code:
Rows(ActiveCell.Row).Offset(Selection.Rows.Count - 1, 0).Copy
Range("A" & ActiveCell.Offset(Selection.Rows.Count, 0).Row).PasteSpecial (xlPasteFormats)
Application.CutCopyMode = False
 

brayleyboy

Board Regular
Joined
Nov 24, 2005
Messages
52
Thanks a lot. This one seemed to work perfectly

Code:
Sub copy_from_above() 
ActiveCell.Offset(Selection.Rows.Count, 0).EntireRow.Insert 
Rows(ActiveCell.Row).Offset(Selection.Rows.Count - 1, 0).Copy 
Range("A" & ActiveCell.Offset(Selection.Rows.Count, 0).Row).PasteSpecial (xlPasteFormats) 
Application.CutCopyMode = False 
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,167,969
Messages
5,856,586
Members
431,822
Latest member
chaftun

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
Top