Go to last Cell of Current Table

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,513
Office Version
  1. 2024
Platform
  1. Windows
Hi Experts

I have multiple tables on one sheet. The following Macro works fine on the first table of the sheet. But when it comes to using it on the any other table of the sheet, it misfires.

Please help
Thanks a lot 🙏

VBA Code:
Sub GoTableLast()
'
' GoTableLast Macro
'

'
    Call GoHome
    
    'Range(ActiveCell, ActiveCell.End(xlToRight)).Select
    ActiveCell.Offset(-1, 0).End(xlToRight).Select
    'ActiveCell.End(xlDown).Select
    
    Dim LRow As Long, LCol As Long
    
    LCol = ActiveCell.Column
    
    
    'To get last row ignoring blank rows in a column
    LRow = Columns(LCol).Find("*", SearchDirection:=xlPrevious, _
    SearchOrder:=xlByRows, LookIn:=xlValues).Row
    
    'To go to last active row of the same Active column
    'Cells(LRow, LCol).Select
    Cells(LRow, LCol).End(xlDown).End(xlDown).End(xlUp).Offset(0, -1).Select
        
End Sub
 
Based on


Here is another option:
VBA Code:
Sub GoToLastRowInColumnActiveTable()

    Dim tblActive As ListObject
    Dim rCol As Range, rLastCell As Range
   
    Set tblActive = ActiveCell.ListObject
    If tblActive Is Nothing Then Exit Sub
   
    Set rCol = Intersect(ActiveCell.EntireColumn, tblActive.Range)

    Set rLastCell = rCol.Find(What:="*", after:=rCol.Cells(1), SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
       
    rLastCell.Select
   
End Sub
@Alex Blakenburg Thanks for helping

With the above code, as per your example #6 it selects F18 while I want it to select H22.
 
Upvote 0

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Missed this...
Rich (BB code):
Sub GoTableLast()
    Dim tbl As ListObject, x
    For Each tbl In ActiveSheet.ListObjects
        If Not Intersect(ActiveCell, tbl.Range) Is Nothing Then
            x = tbl.Parent.Evaluate("max(if(" & tbl.Range.Address & "<>"""",row(1:" & tbl.ListRows.Count + 1 & ")))")
            tbl.Range(x, ActiveCell.Column - tbl.Range(1).Column + 1).Select: Exit For
        End If
    Next
End Sub
@Fuji Thanks for helping and Sorry for the late response.

I didn't miss it. I was taking rest and saw it now.

The challenge with your code is that it takes me to the last row (with data) of the Current active column.

While I want it to go the the last cell of the current table. My table is likely to contain a lot of blank rows with some columns carrying formulae (that are only visible when some data is entered in the row) and some absolutely blank. If I may drop you an idea in post #6 example that @Alex Blakenburg gave, I would like to go to cell H22 when ActiveCell is in Table 2.
 
Upvote 0
Then something like...
Code:
Sub GoTableLast()
    Dim tbl As ListObject
    For Each tbl In ActiveSheet.ListObjects
        If Not Intersect(ActiveCell, tbl.Range) Is Nothing Then
            tbl.Range(tbl.Range.Rows.Count, ActiveCell.Column - tbl.Range(1).Column + 1).Select: Exit For
        End If
    Next
End Sub
 
Upvote 0
You know that makes the column referencing irrelevant right ?!
(you don't actually need the set line)

VBA Code:
Sub GoToLastCellActiveTable()

    Dim tblActive As ListObject
    Dim rLastCell As Range
   
    Set tblActive = ActiveCell.ListObject
    If tblActive Is Nothing Then Exit Sub

    With tblActive.Range
        Set rLastCell = .Cells(.Cells.Count)
        rLastCell.Select
    End With
   
End Sub
 
Upvote 1
Solution
Then something like...
Code:
Sub GoTableLast()
    Dim tbl As ListObject
    For Each tbl In ActiveSheet.ListObjects
        If Not Intersect(ActiveCell, tbl.Range) Is Nothing Then
            tbl.Range(tbl.Range.Rows.Count, ActiveCell.Column - tbl.Range(1).Column + 1).Select: Exit For
        End If
    Next
End Sub
@Fuji This only takes me to the last row of current column
 
Upvote 0
You know that makes the column referencing irrelevant right ?!
(you don't actually need the set line)

VBA Code:
Sub GoToLastCellActiveTable()

    Dim tblActive As ListObject
    Dim rLastCell As Range
  
    Set tblActive = ActiveCell.ListObject
    If tblActive Is Nothing Then Exit Sub

    With tblActive.Range
        Set rLastCell = .Cells(.Cells.Count)
        rLastCell.Select
    End With
  
End Sub
@Alex Blakenburg it works exactly as intended.

Thanks a lot for your patience and help 🙏
 
Upvote 0
Then like my first post
Code:
  tbl.Range(tbl.Range.Rows.Count,1).Select: Exit For
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,083
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top