VBA宏 - 如何在一行中查找多个单词?

时间:2016-12-22 09:19:39

标签: vba excel-vba excel

我有一张包含各种列的工作表,我需要在一行中搜索最多包含3个字的记录。我正在尝试创建一个宏。我写了以下代码,但在两个地方找到了困难:

  1. 如果Activecell不是完全匹配但包含Textbox1中的某些字词

  2. 搜索永远不会结束并进入循环

  3. 我的代码

    Private Sub Findcheck_Click()
    
    Dim rgfound1 As Range
    Dim rgfound2 As Range
    Dim rgfound3 As Range
    
    GoTo msearch
    msearch:
    
    Set rgfound1 = Cells.Find(TextBox1)
    
    Cells.Find(What:=TextBox1, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False).Activate
    
    If rgfound1 Is Nothing Then
        MsgBox "Not Found"
        GoTo mexit
    End If
    
    If ActiveCell.Value = TextBox1 Then
        Rows(ActiveCell.Row).Select
    
        Set rgfound2 = Rows(ActiveCell.Row).Find(TextBox2)
        If rgfound2 Is Nothing Then
            ActiveCell.Offset(1, 0).Select
            GoTo msearch
        End If
    
        Set rgfound3 = Rows(ActiveCell.Row).Find(TextBox3)
        If rgfound3 Is Nothing Then
            ActiveCell.Offset(1, 0).Select
            GoTo msearch
        Else
            MsgBox "Found"
        End If
    
    End If
    
    mexit:
    
    End Sub
    

    enter image description here

3 个答案:

答案 0 :(得分:0)

我尝试使用尽可能多的原始代码,并使用Find方法查找User_From TextBox内的所有3个值,并保留{{ 1}}等...

远离GoTo msearchActivateActiveCell以及使用引用对象SelectSheets("Sheet1") s始终是一个好主意

我还添加了Range语句,以确保代码正在搜索包含您数据的工作表,而不是With

<强>代码

ActiveSheet

Table

我输入搜索的User_Form参数:

enter image description here

结果我运行了上面的代码:

enter image description here

答案 1 :(得分:0)

实现OP代码后编辑的

UserForm ...

你可以试试这段代码:

Private Sub Findcheck_Click()
    Dim rgfound As Range
    Dim firstAddress As String
    Dim rowValsString As String

    With ActiveSheet.UsedRange '<--| reference currently active sheet "used" range only
        Set rgfound = .Find(What:=TextBox1.Text, LookIn:=xlFormulas, _
                                 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                                 MatchCase:=False, SearchFormat:=False) '<--| try finding 'TextBox1' text

        If Not rgfound Is Nothing Then '<--| if found
            firstAddress = rgfound.Address '<--| retrieve first found range address
            Do
                rowValsString = "|" & Join(Application.Transpose(Application.Transpose(.Rows(rgfound.Row).Value)), "|") & "|" '<--| retrieve the found range whole "used" cells content in one string delimiting values with "|"
                If InStr(rowValsString, "|" & TextBox2.Text & "|") > 0 And InStr(rowValsString, "|" & TextBox3.Text & "|") > 0 Then '<--| if other two textboxes texts found in the row values string
                    MsgBox "Found row: " & rgfound.Row
                    Exit Sub
                End If
                Set rgfound = .FindNext(rgfound) '<--| try finding next cell with TextBox1
            Loop While rgfound.Address <> firstAddress '<--| loop until wrapping back on first found range
        End If
    End With
    MsgBox "Not Found"
End Sub

您最好将ActiveSheet替换为实际数据工作表参考,例如WorkSheets("mySheetName")"mySheetName"更改为您的实际数据表名称

答案 2 :(得分:0)

感谢@shai和@ user3598756的帮助

最终代码如下所示

选项明确

Private Sub Findcheck_Click()

Dim Rngfound1 As Range Dim Rngfound2作为范围 Dim Rngfound3 As Range

Dim LastRow As Long,i,x As Long

'将“Sheet1”修改为您工作表的名称(您要在其中搜索3个单词) 使用工作表(“Sheet3”)     'init行号     i = 1

' find last row with data in Column A >> you might need to modify to another column
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

GoTo msearch

msearch:

' loop through all rows from row 2 till last row with data
Set Rngfound1 = .Cells.Find(What:=TextBox1.Value, After:=.Range("A" & i + 1), LookIn:=xlFormulas, LookAt _
            :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
            False, SearchFormat:=False)

If Rngfound1 Is Nothing Then
    MsgBox "Not Found"
    GoTo mexit
Else
    ' successfult find of TextBox1 >> save the row number
    i = Rngfound1.Row
'-
If x = 0 Then
    x = i
    Else
        If x = i Then
            MsgBox "Not Found"
            GoTo mexit
        End If

End If
'-
End If

Set Rngfound2 = Rows(Rngfound1.Row).Find(TextBox2.Value)
If Rngfound2 Is Nothing Then
    GoTo msearch
End If

Set Rngfound3 = Rows(Rngfound1.Row).Find(TextBox3.Value)
If Rngfound3 Is Nothing Then
    GoTo msearch
Else
    Rows(i).Select
    MsgBox "Found on Row No     " & i

End If

结束

MEXIT:

End Sub