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
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
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