我有每周大小不同的数据表。我想删除 D 列中包含字符串“CHF”的行,但同样,D 列的大小每周都会发生变化。我什至不知道从哪里开始。我已经看过写这个问题时推荐的类似问题,但仍然没有弄清楚。有什么解决办法吗?
答案 0 :(得分:0)
Option Explicit
Sub deleteRows()
' Define constants.
Const wsName As String = "Sheet1"
Const cFirst As Long = 2
Const cCol As String = "D"
Const Crit As String = "CHF"
' Define workbook, worksheet, and last row.
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet: Set ws = wb.Worksheets(wsName)
Dim cLast As Long: cLast = ws.Cells(ws.Rows.Count, cCol).End(xlUp).Row
' Combine cells ('cCell') containing Criteria into Delete Range ('drg').
Dim drg As Range
Dim cCell As Range
Dim i As Long
For i = cFirst To cLast
Set cCell = ws.Cells(i, cCol)
If cCell.Value = Crit Then
If drg Is Nothing Then
Set drg = cCell
Else
Set drg = Union(drg, cCell)
End If
End If
Next i
' Delete entire rows of Delete Range in one go.
If Not drg Is Nothing Then
drg.EntireRow.Delete
End If
End Sub
答案 1 :(得分:0)
这是@BigBen前面提到的Filter和Delete的基本前提
Sub FilterDelete()
Application.DisplayAlerts = False
Dim rng As Range
Dim LR as Long
LR=Worksheets("Sheet1").Cells(Rows.Count,1).End(xlUp).Row
Set rng = ThisWorkbook.Worksheets("Sheet1").Range("A1:H"&LR)
rng.AutoFilter Field:=4, Criteria1:="CHF"
rng.Offset(1,0).SpecialCells(xlCellTypeVisible).Delete
AutoFilterMode = False
Application.DisplayAlerts = True
End Sub
您可能有更多或更少的列。您可以使用类似的方法来查找最后一列。没看到你的数据,我举个例子。