How to select the lastrow

我与影子孤独终老i 提交于 2020-01-07 08:16:12

问题


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    45

3   17     18     13    7     45

4   12     45     7     78    78

5   578    54     45    8     78

6   42     72     75    8     78

7   452    22252  2277  87    986

8   752    72     752   878   98638

9   72     72     72    45    78

10  788    72     78    678   465

Now, I want to select the last row in this excel. Because everyday the row number will be change.
But I don't want the Selection.End(xlToRight).Select because in this excel blanks will come after every 2 columns.
Ex:

I want to select A10 to last cell in this row.

I have create “usedrange” method but it doesn’t work well for me.

startcell = Range("B" & Cells.Rows.Count).End(xlUp).Select
endrcell = Range("G" & Cells.Rows.Count).End(xlUp).Select

How can I select the startrow to endcell with blanks?

Please advice.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/20741836/how-to-select-the-lastrow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!