Go to last Cell of Current Table

SanjayGMusafir

Well-known Member
Joined
Sep 7, 2018
Messages
1,515
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
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
What does GoHome do? If it always goes back to a certain cell the code will only work for a specific table only.
 
Upvote 0
@iggydarsa I actually tried thinking of ways on how to tell excel to find last row and last column of the current table. But my knowledge with VBA is not even to the level of a novice :rolleyes:. I only do hit or miss things.

The current vba was working fine because going to ctrl+Home and then one row up would take it to the headers row of first table.

But when it comes to a table somewhere else in the sheet, I went blank :unsure:

If you may help

Thanks a lot 🙏
 
Upvote 0
As I was explaining things in the previous post #4, an idea struck my brain. And I worked as below.

It works most of the times. But there is a glitch. If the current column has some blank cell (which at time shall be) then it jumps to the wrong cell at bottom. And at times it could be far-far away from the last column.

Please help

The macro I evolved is as below

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, CCol As Long, LCol As Long
    
    CCol = ActiveCell.Column
    ActiveCell.End(xlUp).End(xlToRight).End(xlDown).Select
    LCol = ActiveCell.Column
    
    'To get last row ignoring blank rows in a column
    LRow = Columns(CCol).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
 
Upvote 0
Can you clarify what you are actually trying to find ?
In the image below with the ActiveCell being in Table2, do you want:
G20, H20 or H22


1734583830617.png
 
Upvote 0
Perhaps
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, 1).Select: Exit For
        End If
    Next
End Sub
 
Upvote 0
Based on
'To go to last active row of the same Active column

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
 
Upvote 0
'To go to last active row of the same Active column
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
 
Upvote 0

Forum statistics

Threads
1,225,609
Messages
6,185,969
Members
453,333
Latest member
BioCoder84

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