Home » excel » excel – Copy and moving an entire sheet to another workbook. 1mil rows to 65536 rows

excel – Copy and moving an entire sheet to another workbook. 1mil rows to 65536 rows

Posted by: admin April 23, 2020 Leave a comment


The following is part of my code that involves copying an entire named sheet from one master file to a new unsaved file that’s being worked on:

ActiveWorkbook.Sheets("VehicleList").Copy _

So this worked fine to place the sheet into workbook 2 but now the files we’re dealing with are in old excel mode which is throwing up the following error due to old excel having less rows:

“Excel cannot insert the sheets into the destination workbook, because it contains fewer rows and columns”

How can I tweak the copy and pasting into Workbooks(2) without breaking the code? I thought defining a range of 1000 rows to copy and move would work, but this also gave an error. Thanks for your help.

How to&Answers:

Assuming you just want the values (i.e. I’m speeding it up by not doing a copy paste) you can do:

Workbooks(2).Sheets.Add After:=Sheets(1)
Workbooks(2).Sheets(2).Range("A1:F1000").Value = ActiveWorkbook.Sheets("VehicleList").Range("A1:F1000").Value


I’d go like follows

Option Explicit

Sub main()
    Dim targetWs As Worksheet

    With Workbooks("MyWorkbookname").Sheets("VehicleList") '<--| fully qualify reference wanted worksheet in the wanted workbook
        Set targetWs = SetOrGetSheet(Workbooks(2), .name) '<--| get the (new) target worksheet on the target workbook named after referenced sheet
        Intersect(.Range("A1:F1000"), .UsedRange).Copy targetWs.Cells(1, 1) '<--| copy range to target worksheet on the target workbook
        If targetWs.name <> .name Then MsgBox "a new sheet has been created in " & targetWs.Parent.name & " with name " & targetWs.name
    End With
End Sub

Function SetOrGetSheet(targetWb As Workbook, shtName As String) As Worksheet
    targetWb.Worksheets.Add '<--| add a new sheet in the target workbook

    On Error Resume Next
    Set SetOrGetSheet = targetWb.Worksheets(shtName) '<--| try and get any possible target workbook sheet with the passed name
    If SetOrGetSheet Is Nothing Then targetWb.ActiveSheet.name = shtName '<--| if target workbook has no worksheet with passed name then name the new one after it
    Set SetOrGetSheet = targetWb.ActiveSheet 'return the new worksheet
End Function

should you be afraid your range-to-copy could exceed 65 rows and/or 256 columns than you should add its size check

edit for values pasting only

should you be interested in pasting values only then you can go like follows:

Sub main()
    SetOrGetSheet(Workbooks(2), "VehicleList").Range("A1:F1000").value = ActiveWorkbook.Sheets("VehicleList").Range("A1:F1000").value '<--| copy values form source to target worksheet
End Sub

while SetOrGetSheet() function stays the same as above

as you may have guessed that function is there for a more general approach where you may want (or just have) to handle the possibilty of target workbook having a worksheet named after “VehicleList” already