Conditional table row delete (table positioning issue)

dotsent

Board Regular
Joined
Feb 28, 2016
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Ran into an unexpected issue with conditional Table row delete. I have two tables with each checking the other for contents (with ISNUMBER & MATCH formula). I'm trying to delete all table rows from Table MASTER where ID value doesn't exist in Table QUERY - easy enough.

delete.jpg


After launching the code to do this (filtering "FALSE" and deleting the visible table area), I get an error message "This won't work because it would move cells in a table on your worksheet".

The code will run in case I position those tables on top of each other (or split them over to different worksheets). Neither is great for readability (but doable). It just feels a little bogus error message as I still try to delete rows within the single table.

Do you guys have any suggestions how to handle this better or should I just re-position those tables? On the flip side, keeping the code relatively simple is important too. Are there any good alternatives to ditching the autofilter and running some sort of a loop that deletes rows based on condition? Given it's a piece of a bit more extensive code, I'll probably just re-position tables instead of running the loop with the aim to skip autofilter (to keep the code relatively easy).

Code:
Sub DeleteRows()

Dim Tbl As ListObject
Set Tbl = ActiveSheet.ListObjects("MASTER")

Application.DisplayAlerts = False

Tbl.Range.AutoFilter Field:=2, Criteria1:="FALSE"
Tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete

End Sub
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this...

Code:
[color=darkblue]Sub[/color] DeleteRows()
    [color=darkblue]Dim[/color] rng [color=darkblue]As[/color] Range
    
    [color=darkblue]With[/color] ActiveSheet.ListObjects("MASTER")
        .Range.AutoFilter Field:=2, Criteria1:="FALSE"
        [color=darkblue]Set[/color] rng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
        .Range.AutoFilter
        rng.Delete Shift:=xlShiftUp
    [color=darkblue]End[/color] [color=darkblue]With[/color]
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Try this...

Code:
[COLOR=darkblue]Sub[/COLOR] DeleteRows()
    [COLOR=darkblue]Dim[/COLOR] rng [COLOR=darkblue]As[/COLOR] Range
    
    [COLOR=darkblue]With[/COLOR] ActiveSheet.ListObjects("MASTER")
        .Range.AutoFilter Field:=2, Criteria1:="FALSE"
        [COLOR=darkblue]Set[/COLOR] rng = .DataBodyRange.SpecialCells(xlCellTypeVisible)
        .Range.AutoFilter
        rng.Delete Shift:=xlShiftUp
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

Hi AlphaFrog, that looks to be working fine. Thanks!

Could you briefly describe the difference though which makes this code run better? Are there any obvious risks involved? I assume the focus is on:

Code:
.Delete Shift:=xlShiftUp
 
Upvote 0
Deleting while filtered seems to be the snag with a table. This code stores the cells to delete, unfilters, then deletes.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
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