尝试获取End(xlDown)+ 1行时对象所需的错误

时间:2017-07-26 10:20:06

标签: excel vba excel-vba

我有这个代码打开第一个工作簿,第二个工作簿,从第一个工作簿复制范围并将其粘贴到第二个工作簿。我想让它在第二个工作簿中的粘贴范围之后立即选择单元格,但它会导致Object required错误。

Sub tes()
        '**VARIABLES**
        Dim folderPath As String
        folderPath = "Y:\plan_graphs\final\mich_alco_test\files\"
        Dim fileTitle As String
        fileTitle = "5.xlsx"

        Dim dataWorkbook As Workbook
        Set dataWorkbook = Application.Workbooks.Open(folderPath & fileTitle)

        Dim copyRange As Range
        Set copyRange = dataWorkbook.Worksheets("List1").Range("A3:F3", Range("A3").End(xlDown))


        Dim resultWorkbook As Workbook
        Set resultWorkbook = Application.Workbooks.Open("Y:\plan_graphs\final\mich_alco_test\result.xlsx")

        copyRange.Copy
        resultWorkbook.Worksheets("1").Range("A3").PasteSpecial Paste:=xlPasteFormulas

        Dim nextRange As Range
        Set nextRange = resultWorkbook.Worksheets("1").Range("A3:F3", _
        resultWorkbook.Worksheets("1").Range("A3").End(xlDown)).Offset(1, 0).Select

End Sub

我做错了什么?

1 个答案:

答案 0 :(得分:3)

你不能Set范围和Select在同一行,请尝试下面的代码部分:

copyRange.Copy

With resultWorkbook.Worksheets("1")
    .Range("A3").PasteSpecial Paste:=xlPasteFormulas

    Dim nextRange As Range
    Set nextRange = .Range("A3").End(xlDown).Offset(1, 0) ' set the Range first

    nextRange.Select ' <-- select the Range
End With

End Sub