删除 Table 中过滤的行而不是整行
Delete Filtered Rows in Table and not entire Row
当我的 table 被筛选时,如何只删除 table 行而不是整个工作表行?
Dim TradeTable As Excel.ListObject
Set TradeTable = Sheets("Pre Trade").ListObjects("PreTradeTable")
On Error Resume Next
With TradeTable
TradeTable.Select
.Range.AutoFilter Field:=.ListColumns("Ask Spread").Index, Criteria1:=""
Call .DataBodyRange.SpecialCells(xlCellTypeVisible).Select
Selection.Delete
.Range.AutoFilter
End With
On Error GoTo 0
删除 Table 行(不是整行)
Option Explicit
Sub DeleteTableRows()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim TradeTable As ListObject
Set TradeTable = wb.Worksheets("Pre Trade").ListObjects("PreTradeTable")
With TradeTable
.AutoFilter.ShowAllData ' clear possible previous filter
.Range.AutoFilter Field:=.ListColumns("Ask Spread").Index, Criteria1:=""
Dim vrg As Range ' Visible Cells Range
On Error Resume Next ' prevent error if no filtered rows
Set vrg = .DataBodyRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilter.ShowAllData ' this does the trick
If Not vrg Is Nothing Then
vrg.Delete Shift:=xlShiftUp
End If
End With
End Sub
当我的 table 被筛选时,如何只删除 table 行而不是整个工作表行?
Dim TradeTable As Excel.ListObject
Set TradeTable = Sheets("Pre Trade").ListObjects("PreTradeTable")
On Error Resume Next
With TradeTable
TradeTable.Select
.Range.AutoFilter Field:=.ListColumns("Ask Spread").Index, Criteria1:=""
Call .DataBodyRange.SpecialCells(xlCellTypeVisible).Select
Selection.Delete
.Range.AutoFilter
End With
On Error GoTo 0
删除 Table 行(不是整行)
Option Explicit
Sub DeleteTableRows()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim TradeTable As ListObject
Set TradeTable = wb.Worksheets("Pre Trade").ListObjects("PreTradeTable")
With TradeTable
.AutoFilter.ShowAllData ' clear possible previous filter
.Range.AutoFilter Field:=.ListColumns("Ask Spread").Index, Criteria1:=""
Dim vrg As Range ' Visible Cells Range
On Error Resume Next ' prevent error if no filtered rows
Set vrg = .DataBodyRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
.AutoFilter.ShowAllData ' this does the trick
If Not vrg Is Nothing Then
vrg.Delete Shift:=xlShiftUp
End If
End With
End Sub