Finding the next empty Row always seems to ignore the first row of a table. Even if empty. Please Help.

PsychoCube

New Member
Joined
May 27, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi All!,

My First post here..

I am using the follwing string to return the next empty row in a table.
VBA Code:
FindLastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1).Row

If the table is empty, with just the single empty row underneath the column headings, the function using the "FindLastRow"...

VBA Code:
ws.Range("A" & FindLastRow).PasteSpecial Paste:=xlPasteValues

...pastes the data in the row beneath the single empty table row, and thus pasting the data outside of the table.

In case it is relevant, A1 and A2 are also empty cells, the table starts in A3, the empty table row starts in A4, and the data is always pasted in A5...

Thanks for any advice or support in advance :)
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
My guess is that if the table is empty the last row is the first empty row, but then you Offset one row for some reason.
 
Upvote 0
Hi

Welcome to forum

You code likely to work well for data in a Range but as you have discovered, can be a problem when working with tables.

Untested but see if update to your code helps you

Rich (BB code):
    Dim tbl           As ListObject
    Dim FindLastRow   As ListRow
    
    'assumes you only have one table in the worksheet
    Set tbl = ws.ListObjects(1)
    
    'Add New row to the table
    Set FindLastRow = tbl.ListRows.Add(AlwaysInsert:=True)
    
   'perform your copy action AFTER you have added the new row to table
   
   'post copied data to table
   FindLastRow.Range(1).PasteSpecial Paste:=xlPasteValues
    
   'clear clipboard
   Application.CutCopyMode = False

Please ensure that you declare the variables with the data types shown.


Hope Helpful


Dave
 
Upvote 0
Welcome to the Forum.

The issue is that xlUp finds the last row in the table not the last empty row in the table.
Ideally we would use Table/ListObject referencing but based on your post this should work.

VBA Code:
FindLastRow = ws.Columns("A").Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Offset(1).Row
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,141
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