Excel - VBA - 在单元格中搜索特定值

时间:2017-06-15 14:43:17

标签: excel vba cell

是否可以在列中搜索特定值?

我希望能够搜索“B”列中的所有单元格,并在其中查找“word”“pip”(不区分大小写)。我已经得到了其他所有东西,只需知道这是否可能或如何做到。

我的当前代码如下所示:

Sub A()

    ActiveSheet.Name = "Data"

    Dim ws As Worksheet
    Set ws = Sheets("Data")

    Dim ws1 As Worksheet
    Set ws1 = ThisWorkbook.Sheets.Add(After:= _
            ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
    ws1.Name = "pip"

    ws.Activate
    Row = 2

    Dim i As Integer
    For i = 1 To 10
        If (Cells(i, 2).Value = (HAS pip IN IT) Then 'This is the part that i'm struggling with

            Copied = ws.Range(Cells(i, 1), Cells(i, 17)).Value 'If possible, this would cut and paste so it deleted the original
            ws1.Activate
            ws1.Range(Cells(Row, 1), Cells(Row, 17)).Value = Copied

        Row = Row + 1

        ws.Activate

        End If

    Next i
End Sub

编辑:为了澄清,B列中的值永远不会只是“点”。它将是一个完整的句子,但如果它包含“pip”,那么我希望IF函数能够工作。

1 个答案:

答案 0 :(得分:1)

FindFindNext很好地工作(并且很快!)

    '...
    Dim copyRange As Range
    Dim firstAddress As String

    Set copyRange = ws.Range("B1:B1500").Find("pip", , , xlPart)

    If Not copyRange Is Nothing Then
        firstAddress = copyRange.Address
        Do
            ws2.Range(Cells(Row, 1), Cells(Row, 17)).Value = Intersect(copyRange.EntireRow, ws.Columns("A:Q")).Value
            Row = Row + 1
            Set copyRange = Range("B1:B10").FindNext(copyRange)

        Loop While copyRange.Address <> firstAddress
    End If
    '...