hi, I get an error 91 when deleting rows in a dynamic table

jkmclean

Board Regular
Joined
Jan 3, 2019
Messages
65
Office Version
  1. 365
Platform
  1. Windows
I am using the following code to clear a table.
after clearing the table I use the built in entry form to add data to the table and get the error 91.
If I enter some data in the first row of the table, the form works well.

so I tried to enter a 1 in the table after clearing and I get the same error 91. (the line that is bolded)
Sub Clear_Table_Content()
Dim teamroster As ListObject
Worksheets("sheet1").Activate
Set teamroster = ActiveSheet.ListObjects("TeamRoster")
'Define Sheet and table name
With teamroster

'Check If any data exists in the table
If Not .DataBodyRange Is Nothing Then
'Clear Content from the table
.DataBodyRange.Rows.Delete
.DataBodyRange.Range("a1") = 1
End If

End With
'VBA Clear Table Content
end sub

The following code is used to open the built in form in excel 365


Sub openform()
'
' Macro1 Macro
'
Dim lastrow As Long
Dim teamroster As ListObject
Worksheets("sheet1").Activate
Set teamroster = ActiveSheet.ListObjects("TeamRoster")
'Debug.Print teamroster.Rows.Address
' TeamRoster.Range("A"&).Select
lastrow = teamroster.DataBodyRange.Rows.Count

End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Try it like
VBA Code:
Sub Clear_Table_Content()
Dim teamroster As ListObject
'Worksheets("sheet1").Activate
Set teamroster = ActiveSheet.ListObjects("TeamRoster")
'Define Sheet and table name
With teamroster

'Check If any data exists in the table
If Not .DataBodyRange Is Nothing Then
'Clear Content from the table
.DataBodyRange.Rows.Delete
End If
.ListRows.Add.Range(1) = 1
End With
'VBA Clear Table Content
End Sub
 
Upvote 0
Hi That worked.
I had used this .DataBodyRange.Range("a1") = 1 instead of .ListRows.Add.Range(1) = 1 and it didn't work.
Why is that?
Again Thanks.
 
Upvote 0
Because you have delete the databodyrange, so there isn't one.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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