无法从一个工作簿复制和粘贴到另一个(共享驱动器)

时间:2019-08-16 12:55:39

标签: excel vba

我创建了一个工作簿,其中包含一个宏,该宏可以从源文件中复制范围并将其粘贴到目标文件中,两者均位于共享驱动器上。我收到错误消息:运行时错误'438'对象不支持此属性或方法

我知道问题不在于找到工作簿,因为宏崩溃后,它们都在我的桌面上打开了。

这是我的代码:

Dim source As Workbook, source_filepath As String, source_filename As String
Dim destination As Workbook, dest_filepath As String, dest_filename As String, scen_name As String


'Set the location and name of the source file
source_filepath = InputBox("Enter the file path for the source scenario file", "Source file directory")
    If source_filepath = "" Then
        Exit Sub
    End If

source_filename = InputBox("Paste the name of the source scenario file here", "Source file name")
    If source_filename = "" Then
        Exit Sub
    End If


'Set the location and name of the destination file
dest_filepath = InputBox("Enter the file path for the destination scenario file", "Destination file directory")
    If dest_filepath = "" Then
        Exit Sub
    End If

dest_filename = InputBox("Name to give the destination scenario file", "Destination file name")
    If dest_filename = "" Then
        Exit Sub
    End If


Set source = Application.Workbooks.Open(source_filepath & "\" & source_filename & ".xlsx")


Set destination = Workbooks.Add
destination.SaveAs Filename:=dest_filepath & "\" & dest_filename & ".xlsx"

'I tried all these different ways of copy&pasting, they all get the Run-time error '438' Object doesn't support this property or method

source.Sheets(1).Cells(1, 1).Resize(1, 34).Copy destination.Sheet1.Cells(1, 1).Value

source.Sheets(1).Cells(1, 1).Resize(1, 34).Copy _
destination: destination.Sheet1.Cells(1, 1).Value

source.Sheets(1).Cells(1, 1).Resize(1, 34).Copy
destination.Sheet1.Cells(1, 1).PasteSpecial xlPasteValues

1 个答案:

答案 0 :(得分:0)

我对您的代码的最初想法是:

source.Sheets(1).Cells(1,1).Resize(1,34).Copy destination.Sheet1.Cells(1,1).Value

应该是

source.Sheets(1).Cells(1,1).Resize(1,34).Copy destination.Sheet1.Cells(1,1)

您不需要

.Value

最后。 让我知道这是否有效:)