简体   繁体   中英

Excel vba select the last line's cell of the selected range

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 selects the pasted range + the line after it, while I only need a first free cell in column A to be selected.

  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

How would I do that? I imagine it still has something to do with xlDown but can't figure out what.

   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)

Solved it for me (I have 3 upper rows that are not included in this selection, so be wary of using the exact number for your case).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM