Excel vba选择所选范围的最后一行单元格

时间:2017-07-26 11:26:41

标签: excel vba excel-vba

我有这个代码打开第一个工作簿,第二个工作簿,从第一个工作簿复制范围并将其粘贴到第二个工作簿。我想让它在第二个工作簿中的粘贴范围之后选择单元格,但它选择粘贴范围+后面的行,而我只需要选择A列中的第一个空闲单元格。

  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("List").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

        'GET THE LAST RANGE
        Dim nextRange As Range
        Set nextRange = resultWorkbook.Worksheets("1").Range("A3:F3", _
        resultWorkbook.Worksheets("1").Range("A3").End(xlDown)).Offset(1, 0)
        nextRange.Select
End Sub

我该怎么做?我想它仍然与xlDown有关,但无法弄清楚是什么。

1 个答案:

答案 0 :(得分:0)

   Set nextRange = resultWorkbook.Worksheets("1").Range("A3:F3", _
        resultWorkbook.Worksheets("1").Range("A3").End(xlDown)).Offset(1, 0)
   Set nextRange = resultWorkbook.Worksheets("1").Cells(nextRange.Rows.Count + 3, 1)

为我解决了这个问题(我有3个不在此选项中的上行,所以要小心使用你的案例的确切数字。)