调整旧帖子的代码

时间:2017-04-04 17:02:43

标签: excel vba excel-vba

我喜欢这个旧代码来帮助我找到单词:

<div class="container">
    <div class="row">
        <div class="col-xs-3">
            <label class="control-label">Field 1</label>
            <input type="text" class="form-control">
        </div>
        <div class="col-xs-3 ">
            <div class="text-center">                
                <button class="btn btn-primary">Swap!</button>
            </div>
        </div>
        <div class="col-xs-3">
            <label class="control-label">Field 2</label>
            <input type="text" class="form-control">
        </div>
    </div>
</div>

我可以添加更多名称: SearchString =“Mike”&lt; - 喜欢“”Mike“”Andrea“”Etc“

1 个答案:

答案 0 :(得分:0)

或者你可以使用AutoFilter

Option Explicit

Sub Sample()
    Dim aCell As Range
    Dim ws As Worksheet

    Set ws = Worksheets("Sheet1W")

    Dim searchArray As Variant
    searchArray = Array("Mike", "Andrea", "Mary") '<--| list your names to search for

    With ws '<--| reference relevant sheet
        With .Range("A1", .Cells(.Rows.Count, "A").End(xlUp)) '<--| reference its column A cells from row 1 (header) down to last not empty one
            .AutoFilter Field:=1, Criteria1:=searchArray, Operator:=xlFilterValues '<--| filter it with 'searchArray' array values
            If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then '<--| if any filtered cells other then headers
                With .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
                    MsgBox "Names: " & vbCrLf & Join(searchArray, ";") & vbCrLf & vbCrLf & "found in cells " & vbCrLf & .Address
                    For Each aCell In .Cells '<--| loop through your matching cells

                        'your code to handle current matching cell

                    Next
                End With
            End If
        End With
        .AutoFilterMode = False
    End With
End Sub