ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem Deleting all rows from List Object (https://www.excelbanter.com/excel-programming/359245-problem-deleting-all-rows-list-object.html)

Dean[_10_]

Problem Deleting all rows from List Object
 
When I run the subroutine below I get the following error after some rows
are deleted. I can restart the code and it will run for a few rows and error
again. Could someone please tell me what I am doing wrong?
TIA
Dean
---------------------------
Error
---------------------------
1004Application-defined or object-defined error
---------------------------
OK
---------------------------


Sub DeleteAllListRows()
Dim wrksht As Worksheet
Dim objListObj As ListObject
Dim objListRows As ListRows
Dim objLR As ListRow

On Error GoTo ErrorHandler

Set wrksht = ActiveWorkbook.Worksheets("Drawings")
Set objListObj = wrksht.ListObjects(1)
Set objListRows = objListObj.ListRows

For Each objLR In objListRows
If objListRows.Count 0 Then
objLR.Delete
End If
Next


Exit Sub
ErrorHandler:
MsgBox Err.Number & Err.Description, vbOKOnly, "Error"

Resume Next

End Sub



Ivan Raiminius

Problem Deleting all rows from List Object
 
Hi Dean,

change this:
For Each objLR In objListRows
If objListRows.Count 0 Then
objLR.Delete
End If
Next

with a loop:

for i=1 to objListRows.count

....do what you want

next i

Regards,

Ivan



All times are GMT +1. The time now is 10:06 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com