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

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,129,804
Messages
5,638,450
Members
417,025
Latest member
MusterDuster

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