VideoPhoto

26 April, 2021

Find last FILLED Cell/Row/Column in a worksheet

If you need to find last filled cell (entry) located beyond your current view, one efficient way to do that is by using macros, like these three:

Sub FindLastCell()
'Search for any entry, by searching backwards by Rows, then by Columns.
Dim LastColumn As Integer
Dim LastRow As Long
Dim LastCell As Range
 If WorksheetFunction.CountA(Cells) > 0 Then
  LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
    MsgBox Cells(LastRow, LastColumn).Address
    Range(Cells(LastRow, LastColumn).Address).Select
 End If
End Sub

Sub FindLastRow()
'Search for any entry, by searching backwards by Rows.
Dim LastRow As Long
 If WorksheetFunction.CountA(Cells) > 0 Then
  LastRow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    MsgBox LastRow
    Range(Cells(LastRow, "A").Address).Select
 End If
End Sub

Sub FindLastColumn()
'Search for any entry, by searching backwards by Columns.
Dim LastColumn As Integer
Dim ColNum As Long
Dim ColLett As String
 If WorksheetFunction.CountA(Cells) > 0 Then
  LastColumn = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
  ColNum = LastColumn
  ColLett = Split(Cells(1, ColNum).Address, "$")(1)
    MsgBox "Col " & ColNum & "=Column " & ColLett
    Range(Cells("1", LastColumn).Address).Select
 End If
End Sub

You can assign each of the macros to separate Control buttons. If you need help with that, see this link: How to add a button and assign a macro .

To make things easier I've combined all three buttons into one, by moving and grouping them together like you see in this figure:

To find position (address) of my last filled cell, row, or column in my worksheet I just click on appropriate line of text on the button.



No comments:

Post a Comment

All comments are held for moderation. I reserve the right to edit, censor, delete and - if necessary - block comments.