-3

I would like to copy and insert a row, ie row 10 and insert to row 11. I would then like to run that code again (via a command button), to copy row 11 and insert to row 12. I would like to be able to repeat this indefinitely, ie row 12 to row 13 and so on.

I used the record macro function to copy and insert, but it just copies row 10 and inserts to row 11.

Application.CutCopyMode = False

With Worksheets("SUMMARY")

Rows("11:11").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows("10:10").Select
Selection.AutoFill Destination:=Rows("10:11"), Type:=xlFillDefault
Rows("10:11").Select
elmo51
  • 3
  • 1
  • You'll have to get your code to identify the line you want copied somehow. Side note: in general, you want to [avoid using Select](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) in your code. – cybernetic.nomad Jun 05 '23 at 14:46
  • Look for the difference between Relative and Absolute references when using the macro recorder. – Davesexcel Jun 05 '23 at 14:59
  • 2
    Also, search SO (or google) before posting a question. You'll find this is a variation of a common question. It would take very little effort on your part to adapt answers you find to meet your specific needs. – RichardCook Jun 05 '23 at 15:06

1 Answers1

-2

Use the below snippet

Sub CopyAndInsertRow() Dim rowToCopy As Long Dim insertRow As Long

rowToCopy = 10 ' Specify the row number to copy
insertRow = 11 ' Specify the row number to insert

Rows(rowToCopy).Copy
Rows(insertRow).Insert Shift:=xlDown

End Sub

  • 3
    The OP was looking for something more Dynamic. – Kavorka Jun 05 '23 at 14:53
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '23 at 16:16