When I filter a table and try to delete just that row, Excel always asks me if I want to delete the row on the entire worksheet. The only solution I know of is I have to clear the filters, delete that row in that table, and then re-apply the filters. I posted about this a while back and got my answer; no way to do this. I had a thing to do today and I decided that, doggone it, I'm going to get this working and be done with it.
I'm not saying this code is good nor accurate, but it appears to be working at the moment. I only have one filter applied in my table tho. If one of y'all wants to test it out, I'd appreciate the feedback. Also, if there's a way to shorten/improve the code, I'd appreciate the suggestions as well. There's commented out code at the bottom that I left in there in case it's useful for anyone.
Thanks!!
Matt
I'm not saying this code is good nor accurate, but it appears to be working at the moment. I only have one filter applied in my table tho. If one of y'all wants to test it out, I'd appreciate the feedback. Also, if there's a way to shorten/improve the code, I'd appreciate the suggestions as well. There's commented out code at the bottom that I left in there in case it's useful for anyone.
Code:
Sub DeleteFilteredTableRowFinal()
'Include a reference to 'Microsoft Scripting Runtime' for the Scripting.Dictionary stuff
Dim rCell As Range
Dim iRow As Long, iTblRow As Long, DeleteRow As Long
Dim tbl As ListObject
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Long
Dim dict As New Scripting.Dictionary
Dim k As Variant
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet
Set tbl = ws.ListObjects("Table1")
Set rCell = ActiveCell
Application.ScreenUpdating = False
' Step 1: Check to make sure you're in a table
' Step 2: Save out all the filters in the table
' Scripting Dictionary References: [URL unfurl="true"]https://excelmacromastery.com/vba-dictionary/[/URL]
' Collections Reference: [URL unfurl="true"]https://excelmacromastery.com/excel-vba-collections/[/URL]
Set dict = New Scripting.Dictionary
For i = 1 To tbl.AutoFilter.Filters.Count
If tbl.AutoFilter.Filters(i).On Then
' Debug.Print "Storing Filter on column #" & i & ", Name: " & tbl.ListColumns(i).Name
dict.Add tbl.ListColumns(i).Name, _
Array(i, _
tbl.AutoFilter.Filters(i).Criteria1, _
tbl.AutoFilter.Filters(i).Operator)
End If
Next i
' Step 3: Reset the autofilter in the table
tbl.AutoFilter.ShowAllData
' Step 4: Get the row number that you're going to delete
iRow = rCell.Row 'Absolute row number
iTblRow = tbl.Range.Row 'Row number of the header column
DeleteRow = iRow - iTblRow
Debug.Print DeleteRow
' Step 5: Delete that row
tbl.ListRows(DeleteRow).Delete
' Step 6: Reapply the filters to the table
For Each k In dict.Keys()
tbl.Range.AutoFilter Field:=dict(k)(0), Criteria1:=dict(k)(1)
Next k
' Saved/old code for reference:
' Debug.Print "Count of entries in the dict: " & dict.Count
' Loop through all the filters and display
' For i = 1 To tbl.AutoFilter.Filters.Count
' If tbl.AutoFilter.Filters(i).On Then
' Debug.Print "Column Index: " & i
' Debug.Print "Column Name: " & tbl.ListColumns(i).Name
' Debug.Print "Criteria: " & tbl.AutoFilter.Filters(i).Criteria1
' Debug.Print "Operator: " & tbl.AutoFilter.Filters(i).Operator
' End If
' Next i
' Dim v As Variant
' For Each k In dict.Keys()
' Debug.Print k, Join(dict(k), ",") 'Prints out all the details for the key
' Debug.Print dict(k)(0)
' Debug.Print dict(k)(1)
' Debug.Print dict(k)(2)
' For Each v In dict(k)
' Debug.Print v
' Next
' Next k
Application.ScreenUpdating = True
Set rCell = Nothing
Set tbl = Nothing
Set wb = Nothing
Set ws = Nothing
End Sub
Thanks!!
Matt