将数据从一个工作簿复制到另一个 "Object Required"

Copy data from one workbook to another "Object Required"

我目前正在做VBA项目,需要从一个工作簿复制到另一个工作簿,其中WBookPst是我首先打开(使用)的工作簿同时WBookCopy是我打开的工作簿基于我通过将文件中的所有“.xslt”格式列出到我的第一个工作簿的 Sheet1 中获得的链接。这是我的代码:

Sub SortFiles()
    'Set up your variables and turn off screen updating.
    'Dim iCounter As Integer
     Application.ScreenUpdating = False

    'Sort the rows based on the data in column C
    Columns("A:C").Sort key1:=Range("C2"), _
    order1:=xlDescending, Header:=xlYes

    Application.ScreenUpdating = True

    Dim WBookCopy As Workbook
    Dim WBookPst As Workbook
    Dim filePath As String
    Dim sheetName As String
    Dim sheetCopy As Worksheet
    Dim sheetPate As Worksheet
    Dim rngCopy As Range
    Dim rngPst As Range

    filePath = Range("B2").Value
    Set WBookCopy = Workbooks.Open(filePath)

    Columns(30).Insert
    For i = 1 To Sheets.count
        Cells(i, 30) = Sheets(i).Name
    Next i

    sheetName = Range("AD1").Value
    Set sheetCopy = WBookCopy.Worksheets(sheetName)
    Set rngCopy = sheetCopy.Range("A:AA").Copy

    Set WBookPst = ThisWorkbook
    Set sheetPaste = WBookPst.Worksheets("Sheet1").Activate
    Set rngCopy = sheetPaste.Range("A:AA").Select
    ActiveSheet.Paste

End Sub 

Set rngCopy = sheetCopy.Range("A:AA").Copy 处出现错误 "Objects required"。

这是什么意思?
顺便问一下,我在工作表之间复制和粘贴数据的方式是否正确?

问题是 rngCopy 属于范围类型,您不能将其设置为等于方法(复制)。删除 .Copy ,你应该没问题。您也不需要将工作表超出范围设置为变量。您可以只做一行 WBookCopy.SheetName.Range("A:AA").Copy,然后再粘贴另一行。

如@Wyatt 所述 - 您的 copy\paste 语法不正确

这里有两种方法:

Worksheets("Sheet1").Range("A:AA").Copy
Worksheets("Sheet2").Range("A1").PasteSpecial xlPasteAll

Worksheets("Sheet1").Range("A:AA").Copy Destination:=Worksheets("Sheet2").Range("A1")