Skip to content

Instantly share code, notes, and snippets.

@MartinP7r
Last active August 29, 2015 14:01
Show Gist options
  • Save MartinP7r/02685ace6778372cbcea to your computer and use it in GitHub Desktop.
Save MartinP7r/02685ace6778372cbcea to your computer and use it in GitHub Desktop.
returns the (actual) last row of the current Excel Sheet
Function endOfSheet(Optional targetSheet As Worksheet) As Long
'' returns the (actual) last row of the current sheet
' this is far better than using '65536' or whatever the current row size might be,
' especially since .xlsx has 2^20 (1048576), not 2^16.
If targetSheet Is Nothing Then
Set targetSheet = ActiveWorkbook.ActiveSheet
End If
endOfSheet = targetSheet.Rows.Count
End Function
Function lastUsedRow(Optional targetSheet As Worksheet) As Long
'' returns the last used row in the current worksheet
' for columns A:E in this example -- change Range below as needed
' dependencies: endOfSheet() -- actual last row of current sheet
If targetSheet Is Nothing Then
Set targetSheet = ActiveWorkbook.ActiveSheet
End If
lastUsedRow = targetSheet.Range("A" & endOfSheet(targetSheet)).End(xlUp).Row
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment