范围类的复制方法从外部工作簿复制数据失败

时间:2018-06-11 17:00:48

标签: excel vba excel-vba

我正在尝试将表格从外部工作簿复制到我的工作簿,并且出现主题行错误 它出现在rngSourceRange.Copy rngDestination行。

我有什么遗失的吗?

 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

提前致谢!

1 个答案:

答案 0 :(得分:2)

您应该只需要目的地的左上角单元格。使用左上角的单元格可以避免尺寸不匹配问题。由于您以后将rngDestination用于其他目的,因此在将其用作目标时只需使用.cells(1)。

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

我并不完全清楚您收到了哪些通知和警告,但您可以在代码顶部尝试此操作。

application.displayalerts = false

然后在退出子程序之前使用以下内容再次打开它们。

application.displayalerts = true