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

时间:2016-06-09 16:18:35

标签: excel vba excel-vba macros

我做了一些编程已经有一段时间了,而且我对一个简单的excel vba宏没有太多运气。我在列中有数据,我需要选择然后删除不包含特定值的单元格。我的数据在列中显示如下:

990ppbAu / 1,2
990ppbAu / 0,5
990ppbAu / 0,5
990ppbAu / 0,3
9900ppmZr / 29,1
9900ppmZn / 5,2
9900ppmZn / 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条记录。提前谢谢!

1 个答案:

答案 0 :(得分: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