找到VBA Textbox值循环vlookup的次数值

时间:2015-08-21 14:04:51

标签: excel vba excel-vba

我正在电子表格中创建一个表格,其中包含测验的类别,问题和答案。 向用户显示一个表单,允许他们轻松地在工作簿中导航,这还包括一个文本框选项,如果他们不确定问题/答案可能属于哪个类别,则允许他们搜索短语。

我已经生成了一个vlook,可以从不同工作表上的类别/问题和答案表中提取。 我还生成了一个计数,因此我能够确定这项工作在测验表中出现的次数。

我的问题是我正在努力开发一个循环,以便如果找到关键短语6次,我希望将6个问题和答案列出给用户。目前它只是拉动它的最后时间。

我目前的代码包括以下内容:

Private Sub CommandButton1_Click()
    If Len(search_text) = 0 Then
        MsgBox "Please enter a key word to search for!", vbCritical
    End If

    Dim wordCount As Integer
    wordCount = Application.WorksheetFunction.CountIf(Sheet1.Range("A2:c600"), "*" & search_text.Value & "*")
    'Else: wordCount = WorksheetFunction.CountIf(Sheet1.Range("A2:c600"), search_text.Value)

    If wordCount = 0 Then
        MsgBox "No match found"
    Else
        Sheet2.Range("a7").Value = WorksheetFunction.VLookup("*" & search_text.Value & "*", Sheet1.Range("A2:c600"), 3, False)
        Sheet2.Range("b7") = wordCount
    End If

End Sub

关于实现循环以及允许一个接一个地打印问题/答案的任何建议将非常感激。 我已经阅读了很多关于此问题的其他问题页面,似乎没有一个与我想要做的相符。

非常感谢提前

1 个答案:

答案 0 :(得分:1)

我使用FindFindNext的组合来搜索search_text输入字段中输入的字词的一系列单元格。我在代码中添加了注释,以便更好地帮助您了解到底发生了什么。

当您找到结果时,我不确切知道您需要对结果做些什么,现在我只显示一个显示匹配的消息框。如果您想在评论中澄清您想要什么,我们可以研究如何对结果进行实际操作。

此代码假设您有一个名为Results

的工作表
Private Sub CommandButton1_Click()

        Dim rngResult As Range
        Dim strFirstAddress As String
        Dim i As Long

        If Len(search_text.Text) = 0 Then
                MsgBox "Please enter a key word to search for!", _
                        vbCritical

                'Stop code exeuction if no search
                'term is entered
                Exit Sub
        End If

        'Clear the previous results range
        Sheets("Results").Range("A2:C600").ClearContents

        'Set i to row 2 of the results worksheet
        i = 2

        'Look in range A2:C600 of Sheet1
        With Sheet1.Range("A2:C600")

                'Perform the initial find
                Set rngResult = .Find(What:=search_text.Text, LookAt:=xlPart)
                        'Check to ensure that the term is found
                If Not rngResult Is Nothing Then
                        'Grab the cell address of the first match
                        'This will help to avoid an infinite loop
                        strFirstAddress = rngResult.Address
                        'Continue Searching
                        Do

                                'Display the output to you
                                'MsgBox "Matched '" & search_text.Text & "' to " & rngResult.Value & " in cell " & rngResult.Address

                                'Put the result on the results page
                                Sheets("Results").Range("A" & i & ":C" & i).Value = Range("A" & rngResult.Row & ":C" & rngResult.Row).Value
                                i = i + 1

                                'Move on to the next result
                                Set rngResult = .FindNext(rngResult)

                        'Break out of the loop when we return to the starting point of the search
                        Loop While Not rngResult Is Nothing And rngResult.Address <> strFirstAddress
                End If
        End With

        'Clean up variables
        Set rngResult = Nothing

End Sub