Deleting visible rows in a table. (Failing to execute)

Darranimo

Board Regular
Joined
Jan 19, 2022
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I have written the vba code below to filter a table field (account) for the value 2010 and then delete all visible data rows. I receive a run-time error 1004 on the following line:

VBA Code:
        VisibleRows.EntireRow.Delete

However, if change that line to this:

VBA Code:
        VisibleRows.Delete

Excel will ask me if I want to delete the entire row. Upon saying yes the code finishes executing perfectly. Is anyone able to tell me what is wrong with my code or why it won't execute when ".EntireRow.Delete" is used? Your help would be greatly appreciated! Here is the code as it stands right now:

VBA Code:
Sub DeleteRows2010()
    Dim tbl As ListObject
    Dim AccountColumn As Range
    Dim FilterRange As Range
    Dim VisibleRows As Range
    
    Application.ScreenUpdating = False
    Set tbl = Worksheets("GL Balance").ListObjects("tbl_GLBalance")
    Set AccountColumn = tbl.ListColumns("Account").Range
    
    Set FilterRange = tbl.Range
    
    tbl.Range.AutoFilter Field:=tbl.ListColumns("Account").Index, Criteria1:="2010"
    
    Set VisibleRows = tbl.DataBodyRange.Offset(1).Resize(tbl.DataBodyRange.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
        
    If Not VisibleRows Is Nothing Then
        VisibleRows.Delete
    End If

    tbl.AutoFilter.ShowAllData
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You need to ShowAllData for the listobject first. Then you can delete the rows. So, from my test workbook:

Code:
tbl.AutoFilter.ShowAllData
If Not VisibleRows Is Nothing Then
    VisibleRows.Delete
End If
 
Upvote 1
Solution
You need to ShowAllData for the listobject first. Then you can delete the rows. So, from my test workbook:

Code:
tbl.AutoFilter.ShowAllData
If Not VisibleRows Is Nothing Then
    VisibleRows.Delete
End If
Oh my god! Thank you so much! I was losing my mind. Lol
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,161
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