VBA Deleting ListObjects Rows With Hidden Columns

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I am writing a macro to delete all rows of a ListObject table not matching a specific value, so that the resulting table is left only with the value I want to keep. I am running into problems when the first or last column of the table is hidden, and cannot figure out what is going on or what the solution is.

A simplified version of my table is

Index
Col1
Col2
Col3
Name
Col5
1
xxx
xxx
xxx
Anne
xxx
2
xxx
xxx
xxx
Bob
xxx
3
xxx
xxx
xxx
Steve
xxx
4
xxx
xxx
xxx
Chris
xxx
5
xxx
xxx
xxx
John
xxx
6
xxx
xxx
xxx
Anne
xxx
7
xxx
xxx
xxx
Anne
xxx
8
xxx
xxx
xxx
Anne
xxx
9
xxx
xxx
xxx
Anne
xxx

<tbody>
</tbody>


Given the above, the column I want to delete based on is name. So my macro so far is

Code:
Sub DeleteRows()
    Application.DisplayAlerts = False
    Dim ListObj As ListObject
    Set ListObj = ActiveSheet.ListObjects(1)
    
    With ListObj
    
        .Range.Sort _
            Key1:=.ListColumns("Name"), _
            Order1:=xlAscending, _
            Header:=xlYes, _
            OrderCustom:=1, _
            MatchCase:=False, _
            Orientation:=xlTopToBottom
            
        .Range.AutoFilter Field:=.ListColumns("Name").Index, Criteria1:="<>" & "Anne"
        .DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Delete
        .AutoFilter.ShowAllData
    
    End With
    
    Application.DisplayAlerts = True
End Sub

That works fine, unless either the first or last column of the table is hidden. So say I create the table above, hide the "Index" column, and then run the macro, it deletes every column of the table (including headers) except for the hidden column. The same is true if the last column is hidden. However, columns hidden in the middle of the table do not cause similar problems.

Furthermore, it only is an issue for the first sorted item of the list. So back to my example, "Anne" is the first item in the sorted list. If I hide the first/last column and delete rows not matching "Bob," there is no issue.

I cannot understand why this is happening, as I thought this line

.DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Delete

Would tell the macro to only delete the DataBodyRange (not headers) and rows.

Any ideas on what the issue is with this?

Thanks
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try replacing...

Code:
.DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Delete

with

Code:
.DataBodyRange.Delete

Hope this helps!
 
Upvote 0
Try replacing...

Code:
.DataBodyRange.SpecialCells(xlCellTypeVisible).Rows.Delete

with

Code:
.DataBodyRange.Delete

Hope this helps!

Thanks! I'm a bit confused though, I thought that would delete the filtered cells as well, which is why I thought selecting visible cells only was necessary. Or does that only apply to hidden, not filtered, data?
 
Upvote 0
In this context, where the data is filtered, it will delete only visible cells.
 
Upvote 0

Forum statistics

Threads
1,216,084
Messages
6,128,726
Members
449,465
Latest member
TAKLAM

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