Object required error when trying to get End(xlDow

2019-09-16 04:59发布

I have this code that opens first workbook, second workbook, copies a range from the first one and pastes it into the second one. I want to make it select the cell right after the pasted range in the second workbook, but it failes with Object required error.

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

What am I doing wrong?

1条回答
劳资没心,怎么记你
2楼-- · 2019-09-16 05:22

You can't Set the range and Select it in the same line, try the code section below:

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
查看更多
登录 后发表回答