[英]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)
在我的代码中,我有一个在我的团队中每月更新的大表,我想做的是找到名为“RD”的列 header,然后删除该列中包含值“Ad-Hoc”的所有行(除了从列标题)
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.您需要使用
tbl.ListColumns.Count
列表对象没有列属性。
You need to assign finalRow
a value, and you have your row and column swapped looking for "Ad-Hoc"您需要为
finalRow
分配一个值,然后交换行和列以查找“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
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.