How to select the lastrow

前端 未结 2 1355
后悔当初
后悔当初 2021-01-29 04:13

I have an excel sheet it contains data as below:

    A      B      C     D     E      F           
1   10     11     12    78    45

2   12     15     15    78           


        
相关标签:
2条回答
  • To Select the last row with data, first move upwards from the bottom to find the row and then move leftwards on that row to find that last used column:

    Sub SelectLastRow()
        Dim nRow As Long, nColumn As Long
        nRow = Cells(Rows.Count, "A").End(xlUp).Row
        nColumn = Cells(nRow, Columns.Count).End(xlToLeft).Column
        Range(Cells(nRow, "A"), Cells(nRow, nColumn)).Select
    End Sub
    
    0 讨论(0)
  • 2021-01-29 04:31

    How about this:

    Sub GetLastRow()
        Dim rng As Range, lastRow As Long, col As Long
    
        lastRow = Range("A1").End(xlDown).Row //Get last row in column A
        col = Range("XFD" & lastRow).End(xlToLeft).Column //Get last used column in last row
    
        Set rng = Range(Cells(lastRow, 1), Cells(lastRow, col))
    End Sub
    
    0 讨论(0)
提交回复
热议问题