简体   繁体   中英

Delete Rows in specific columns based on values

I got quite a specific task that im not getting quite right, wondering if anyone could help me out. In my code, I have a big table that gets updated monthly amongst my team, what I want to do is find the column header titled "RD" and then delete all the rows within that column containing the value "Ad-Hoc" (apart from the column header)

Sub Delete_Rows_Based_On_Value()

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("Table_owssvr")

    Dim I As Long, finalRow As Long, L As Long
    
For L = tbl.Columns.Count To 1 Step -1
                If Cells(1, L) = "RD" Then
                For I = finalRow To 2 Step -1
                If Range(L, I).Value = "Ad-Hoc" Then
            Range(L, I).EntireRow.Delete
        End If
    Next I
    End If
    Next L

End Sub

wonder if anyone could help me with this and whether im on the right track. thanks

You're on the right track. You need to use tbl.ListColumns.Count listobjects don't have a columns property.

You need to assign finalRow a value, and you have your row and column swapped looking for "Ad-Hoc"

Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("Table_owssvr")

Dim I As Long, finalRow As Long, L As Long
    
For L = tbl.ListColumns.Count To 1 Step -1 'tbl.columns will error
    If Cells(1, L) = "RD" Then
        finalRow = Cells(Rows.Count, L).End(xlUp).Row 'Get the last row in column L
        For I = finalRow To 2 Step -1
            If Cells(I, L).Value = "Ad-Hoc" Then 'L is the column it goes second
                Cells(I, L).EntireRow.Delete
            End If
        Next I
    End If
Next L

Using Autofilter

Sub delete_with_filter()
    Dim tbl As ListObject
    Dim delrange As Range
    Set tbl = ActiveSheet.ListObjects("Table_owssvr")

    With tbl
        .Range.AutoFilter .ListColumns("RD").Index, "Ad-Hoc"
        On Error GoTo errhandler
        Set delrange = .DataBodyRange.SpecialCells(xlCellTypeVisible)
        If Not delrange Is Nothing Then
            Application.DisplayAlerts = False
            delrange.Delete
            Application.DisplayAlerts = True
        End If
        .Range.AutoFilter .ListColumns("RD").Index
        
    End With
errhandler:
    Select Case Err.Number
        Case 1004
            Debug.Print "Exiting Sub, No Cells Found"
            tbl.Range.AutoFilter tbl.ListColumns("RD").Index
            Exit Sub
    End Select

End Sub

Delete Rows in Table

  • This will delete table rows (not worksheet rows).

The Code

Option Explicit

Sub deleteRowsBasedOnValue()
    With ThisWorkbook.Worksheets("Sheet1").ListObjects("Table_owssvr")
        Dim cNum As Long: cNum = .ListColumns("RD").Index
        Dim dRng As Range
        Dim lr As ListRow
        For Each lr In .ListRows
            If lr.Range.Columns(cNum).Value = "Ad-Hoc" Then
                buildRange dRng, lr.Range
            End If
        Next lr
    End With
    If Not dRng Is Nothing Then
        dRng.Delete
    End If
End Sub

Sub buildRange( _
        ByRef BuiltRange As Range, _
        AddRange As Range)
    If BuiltRange Is Nothing Then
        Set BuiltRange = AddRange
    Else
        Set BuiltRange = Union(BuiltRange, AddRange)
    End If
End Sub

A lot answers, but I will throw this one in for short and quick. I tested and worked.

Sub DeleteTableRows()

    Dim tbl As ListObject
    
    Set tbl = ActiveSheet.ListObjects("Table_owssvr")
    
    tbl.Range.AutoFilter Field:=tbl.ListColumns("RD").Index, Criteria1:="Ad-Hoc"
    If tbl.Range.SpecialCells(xlCellTypeVisible).Count > tbl.ListColumns.Count Then
        Application.DisplayAlerts = False
        tbl.DataBodyRange.SpecialCells(xlCellTypeVisible).Delete
        Application.DisplayAlerts = True
    End If
     
    tbl.AutoFilter.ShowAllData
    
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM