Lastrow in VBA no longer works with Table

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I have a piece of VBA that identifies the last row in a column, so that I can assign values:

VBA Code:
Lastrow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Range("A" & Lastrow).Offset(1, 1).Select
BankNo = ActiveCell

My issue, is I recently formatted the data on the worksheet as a table, so that visually it is easy to read and edit, and to delete and add rows. Now, my Lastrow doesn't work as it selects the row after the end of the table instead of the first blank row in Column A. Is there something simple I can change in my code to look for my blank values in the table?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Test the last cell in the relevant table column. If it's blank, do your End(xlUp) from there.

Seems a slightly odd thing to need to do in a table though.
 
Upvote 0
Test the last cell in the relevant table column. If it's blank, do your End(xlUp) from there.

Seems a slightly odd thing to need to do in a table though.
I'm not sure how to go about that. I tried adding the tablename in place of the sheet, but that only threw an error.
 
Upvote 0
For example:

VBA Code:
   Dim lastRow As Long
   With ActiveSheet.ListObjects(1).ListColumns(1).DataBodyRange.Cells
      If IsEmpty(.Item(.Count)) Then
         lastRow = .Item(.Count).End(xlUp).Row + 1
      Else
         lastRow = .Item(.Count).Row + 1
      End If
   End With

lastRow will be the row number after the last row with data (so the row underneath the table if the last cell in that table column has data already).
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,126
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