在 Excel 中搜索列,查找值,选择并删除

问题描述 投票:0回答:1

自从我完成一些编程以来已经有一段时间了,但我对简单的 Excel VBA 宏没有太多运气。我的列中有数据,我需要选择然后删除不包含特定值的单元格。我的数据在一列中如下所示:

990ppbAu/1,2
990ppbAu/0,5
990ppbAu/0,5
990ppbAu/0,3
9900ppmZr/29,1
9900ppmZn/5,2
9900ppm锌/1
9900ppmZn/0,8
9900ppmZn/0,5
9900ppmCu/2,8

我需要删除其中不包含“Au”的值或字符串。这是我的代码的开始,它不多,而且可能是错误的......但我希望有人能指出我正确的方向:

Option Explicit

Sub SearchColumn()
    Dim strAu As String
    Dim rngFound, rngDelete As Range
    strAu = "*Au*"

    'Search Column
    With Columns("AF")
    'Find values without "Au" in string in column AF and delete.
        Set rngFound = .Find(strAu, .Cells(.Cells.Count), xlValues, xlWhole)
        If rngFound <> strAu
        'Then select the value and delete
        'Else move onto the next cell until end of the document
    End With
End Sub

我应该注意,有些单元格中没有任何内容,而有些单元格具有如上所示的字符串值。我需要它遍历整个列直到文档结束。表中约有 115,000 条记录。预先感谢!

excel vba
1个回答
1
投票

Edited3 仅删除单元格内容

编辑2使其删除单个单元格

编辑以“反转”之前的过滤标准并保留包含“Au”的单元格

如果您的列有标题,那么您可以使用以下代码:

Option Explicit

Sub SearchColumnWithHeader()
    Dim strAu As String

    strAu = "Au"
    With ActiveSheet
        With .Range("AF1", .Cells(.Rows.Count, "AF").End(xlUp))
            .AutoFilter Field:=1, Criteria1:="<>*" & strAu & "*"
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).ClearContents 
        End With
        .AutoFilterMode = False
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.