代码突然停止工作 运行-时间错误“1004”范围 class 的 PasteSpecial 方法失败

Code suddenly stopped working Run-Time Error "1004" PasteSpecial method of Range class failed

我的代码突然停止工作,我不明白为什么。我是 VBA.

的新手
Sub Get_Data_From_File()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
Application.ScreenUpdating = False
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="ExcelFiles(*xls*),*xls*")
If FileToOpen <> False Then
    Set OpenBook = Application.Workbooks.Open(FileToOpen)
    OpenBook.Sheets(1).Range("A1:S250").Copy
ThisWorkbook.Worksheets("RecognitionsLog").Range("A2").PasteSpecial xlPasteValues
OpenBook.Close False


End If
Application.ScreenUpdating = True
End Sub

试试这个:

Set destinationRange = ThisWorkbook.Worksheets("RecognitionsLog").Range("A2")
destinationRange.PasteSpecial Paste:=xlPasteValues

从关闭的工作簿中复制值

  • 你的代码在我这边运行良好,但我更喜欢通过赋值来复制值。试一试,如果问题仍然存在,请告诉我们。
Option Explicit

Sub CopyValues()
    
    Application.ScreenUpdating = False
    
    Dim FileToOpen As Variant
    FileToOpen = Application.GetOpenFilename( _
        Title:="Browse for your File & Import Range", _
        FileFilter:="ExcelFiles(*.xls*),*.xls*")
    
    If FileToOpen <> False Then
        Dim OpenBook As Workbook: Set OpenBook = Workbooks.Open(FileToOpen)
        Dim srg As Range: Set srg = OpenBook.Worksheets(1).Range("A1:S250")
        Dim drg As Range: Set drg = ThisWorkbook.Worksheets("RecognitionsLog") _
            .Range("A2").Resize(srg.Rows.Count, srg.Columns.Count)
        drg.Value = srg.Value
        OpenBook.Close False
    End If
    
    Application.ScreenUpdating = True

End Sub