用vba查找范围内的文本

时间:2014-11-20 22:49:07

标签: excel vba excel-vba

我的范围包含不同列中的不同单词,范围为DT21:EH400。我想知道是否有办法搜索该范围,如果有一个单词复制并将其粘贴到它所在的同一行,但在B列中。

2 个答案:

答案 0 :(得分:0)

内置的Find功能应该比编写自己的循环更快:

Sub findUsingFIND()

    Dim searchString As String
    searchString = Excel.Application.InputBox("Enter string please")

    Dim targetArea As Range
    Set targetArea = Excel.Application.InputBox("Select range to search", , , , , , , 8)

    targetArea.Select
    'Excel.ThisWorkbook.Sheets(1).Range("DT21:EH400").Select

    Dim foundRange As Range
    With targetArea
        Set foundRange = _
            .Find(What:=searchString, _
                  After:=.Cells(1), _
                  LookIn:=xlValues, _
                  LookAt:=xlWhole, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlNext, _
                  MatchCase:=False)
    End With

    If Not foundRange Is Nothing Then
        ThisWorkbook.Sheets(1).Range("B" & foundRange.Row) = searchString 
    Else
        MsgBox "Nothing found"
    End If

End Sub

如果有多个字符串实例,则上述内容可以适用于以下内容:

Sub findSeveralUsingFIND()

    Dim searchString As String
    searchString = Excel.Application.InputBox("Enter string please")

    Dim targetArea As Range
    Set targetArea = Excel.Application.InputBox("Select range to search", , , , , , , 8)

    targetArea.Select
    'Excel.ThisWorkbook.Sheets(1).Range("DT21:EH400").Select

    Dim foundRange As Range
    With targetArea
        Set foundRange = _
            .Find(What:=searchString, _
                  After:=.Cells(1), _
                  LookIn:=xlValues, _
                  LookAt:=xlWhole, _
                  SearchOrder:=xlByRows, _
                  SearchDirection:=xlNext, _
                  MatchCase:=False)

        If Not foundRange Is Nothing Then
            FirstAddress = foundRange.Address
            Do
                ThisWorkbook.Sheets(1).Range("B" & foundRange.Row).Value = searchString
                Set foundRange = .FindNext(foundRange)
            Loop While Not foundRange Is Nothing And foundRange.Address <> FirstAddress
        Else
            MsgBox "Nothing found"
        End If

    End With

End Sub

正如@ChrisNeilsen所指出的那样,如果你需要多次执行搜索,有一种非常快的方法会更好。这使用数组。我的理解相对较浅,为什么这种方法很快,但我认为它与数组将数据存储在彼此相邻的内存地址块中的方式有​​关。以下是不同方法的良好比较:

http://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/

这是使用变体类型数组的宏:

Sub findUsingVARARRAY()

    Dim vArr As Variant, vRes As Variant
    Dim j As Long
    Dim n As Long

    Dim searchString As String
    searchString = Excel.Application.InputBox("Enter string please")

    Dim targetArea As Range
    Set targetArea = _
      Excel.Application.InputBox(prompt:="Select range to search", Type:=8)

    Dim firstRow As Long

    vArr = targetArea.Value2
    ReDim vRes(LBound(vArr, 1) To UBound(vArr, 1), 1 To 1)
    Dim r As Long, c As Long
    For r = LBound(vArr, 1) To UBound(vArr, 1)
    For c = LBound(vArr, 2) To UBound(vArr, 2)
        ' use vbTextCompare for case insenstitive comapre
        ' use vbBinaryCompare for case senstitive comapre
        If StrComp(vArr(r, c), searchString, vbTextCompare) = 0 Then
            vRes(r, 1) = searchString
            Exit For
        End If
    Next c, r

    targetArea.EntireRow.Columns(2) = vRes
End Sub

答案 1 :(得分:-1)

以下代码可能有效

搜索范围中的单词,并将搜索词设置为搜索单词所在行的B列中的值

Dim strSearch as String
Dim rngData as Range
Dim rngCell as Range

strSearch = "Word to Search"
Set rngData = Range(Cells(21,124),Cells(400,138))

For each rngCell in rngData
    If rngCell.value = strSearch Then
        cells(rngcell.Row, 2).Value = strSearch
    End if
Next rngCell