Use insert instead of copy

Pau905

New Member
Joined
Nov 10, 2020
Messages
16
Office Version
  1. 365
Hello, I got a problem with the code below. When copy is used the rows will be shifted down (I do not want that), I would like to use insert instead, but how?

/Pau


Code:
    'Copy the data of the first row (header)
    Set RangeOfHeader = ThisSheet.Range(ThisSheet.Cells(1, 1), ThisSheet.Cells(1, NumOfColumns))

    For p = 2 To ThisSheet.UsedRange.Rows.Count Step RowsInFile - 1
        Set wb = Workbooks.Add
      
        'Paste the header row in new file
        RangeOfHeader.Copy wb.Sheets(1).Range("A1")
    
        'Paste the chunk of rows for this file
        Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 2, NumOfColumns))
        RangeToCopy.Copy wb.Sheets(1).Range("A2")
              
        'Save the new workbook, and close it
        wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
        wb.Close
    
      'Increment file counter
      WorkbookCounter = WorkbookCounter + 1
    Next p
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Not quite sure what you mean by
When copy is used the rows will be shifted down
You are copying the data to A2 every time & it does not "shift" anything.
 
Upvote 0
Hello Fluff,

I solved the problem by using paste instead. With copy I got extra rows at the end.

Thanks for a super forum!
 
Upvote 0
Glad you sorted it & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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