Home » excel » excel – Copy Method of Range Class Failed copying data from external workbook

excel – Copy Method of Range Class Failed copying data from external workbook

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to copy a table from an external workbook to my workbook and am getting the subject line error.
It occurs on the rngSourceRange.Copy rngDestination line.

Is there anything I am missing?

 Sub ImportLogInfo()
    Dim wkbCrntWorkBook As Workbook
    Dim wkbSourceBook As Workbook
    Dim rngSourceRange As Range
    Dim rngDestination As Range

    Set wkbCrntWorkBook = ActiveWorkbook

            Set wkbSourceBook = Workbooks.Open(Filename:="C:\Users\Documents\Proposal Log Feeder\Modular Proposals Log.xlsb", ReadOnly:=True)
            Set rngSourceRange = wkbSourceBook.Sheets("Log").Range("DBtable")
            wkbCrntWorkBook.Activate
            Set rngDestination = wkbCrntWorkBook.Sheets("Log").Range("DBtable3")
            rngSourceRange.Copy rngDestination
            rngDestination.CurrentRegion.EntireColumn.AutoFit
            wkbSourceBook.Close False
End Sub

Thanks in Advance!

How to&Answers:

You should only require the top-left cell of a destination. Using the top-left cell avoids size mismatch problems. Since you use rngDestination for other purposes later, just use .cells(1) when using it as the destination.

...
Set rngSourceRange = wkbSourceBook.Sheets("Log").Range("DBtable")
wkbCrntWorkBook.Activate
Set rngDestination = wkbCrntWorkBook.Sheets("Log").Range("DBtable3")
rngSourceRange.Copy rngDestination.cells(1)
---

I’m not entirely clear on which notifications and warnings you are receiving but you can try this at the top of your code.

application.displayalerts = false

Then turn them back on again with the following before exiting the sub procedure.

application.displayalerts = true