【问题标题】:Delete row based on partial text根据部分文本删除行
【发布时间】:2012-07-04 06:06:25
【问题描述】:

如果我在 A 列中找到文本“FemImplant”,我会尝试删除整行。

文本是由“$”链接的句子的一部分。我需要解析'$'之前的单元格内容,看看它是否匹配'FemImplant'并删除该行。

这是我目前所拥有的。

Dim cell As Excel.Range
RowCount = DataSheet.UsedRange.Rows.Count
Set col = DataSheet.Range("A1:A" & RowCount)
Dim SheetName As String
Dim ParsedCell() As String

For Each cell In col
    ParsedCell = cell.Value.Split("$")
    SheetName = ParsedCell(0)

    If SheetName = "FemImplant" Then
        cell.EntireRow.Delete Shift:=xlUp
    End If
Next

【问题讨论】:

  • 我遇到的主要问题是,我无法让它从底部循环。我设置它的方式是从顶部循环,它不会删除连续的行。

标签: excel vba


【解决方案1】:

您可以使用 AutoFilter 删除包含文本 FemImplant$ 的行。这种方法会比循环快得多。

如果您使用的是 Boolean 值,那么您可能希望查看 Trying to Delete Rows with False Value in my Range

看这个例子

我假设单元格 A1 有标题。

Sub Sample()
    Dim ws As Worksheet
    Dim strSearch As String
    Dim lRow As Long
    
    strSearch = "FemImplant$"
    
    Set ws = Sheets("Sheet1")
    
    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row
        
        '~~> Remove any filters
        .AutoFilterMode = False
        
        '~~> Filter, offset(to exclude headers) and delete visible rows
        With .Range("A1:A" & lRow)
          .AutoFilter Field:=1, Criteria1:="=*" & strSearch & "*"
          .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        End With

        '~~> Remove any filters
        .AutoFilterMode = False
    End With
End Sub

快照

【讨论】:

  • 完美。谢谢!
  • 好答案。更快的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多