Excel 2010 - Last Row in a Table

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Good afternoon everyone!

I'm having a bit of an issue working with Tables in Excel 2010 (first time using them, and first time using VBA with them as well). When the table is empty (contains only headers), my VBA last row statement is returning the row under the headers, not the header row.

For example, if my headers are in Row 1, and my table is blank, using the statement:

Code:
LR = Range("A" & Rows.Count).End(xlUp).Row

This returns 2, not the expected 1 that I would get if the sheet was not set up as a table.

However, when I populate the first row (ie row 2 now contains data), then the same statement above returns the expected 2. And when row n is populated, the statement returns the expected value of n.

Is there another method or declaration I need for the VBA code to properly return the "last row" of my table, even when the first data row is blank without doing some nasty workaround?

Thanks much!

~Keith
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello Keith,

If by Table you mean a List Object then the macro below will work. It has error handling in case there is no header. You will need to change the subscript to match your ListObject's index.
Code:
Dim R As Long
Dim Rng As Range

    On Error Resume Next
    Set Rng = ActiveSheet.ListObjects(1).Range
    
    If Err = 0 Then
       R = Rng.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
    Else
       R = 0
    End If
 
Upvote 0
Thanks Leith, I'll give this a shot in a tick.
 
Upvote 0
Hello Keith,

If by Table you mean a List Object then the macro below will work. It has error handling in case there is no header. You will need to change the subscript to match your ListObject's index.
Code:
Dim R As Long
Dim Rng As Range
 
    On Error Resume Next
    Set Rng = ActiveSheet.ListObjects(1).Range
 
    If Err = 0 Then
       R = Rng.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
    Else
       R = 0
    End If

Thank you for sharing this code. It worked perfect for me.

M.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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