Searching a Textbox within a Userform for key words

Qmustard

New Member
Joined
Sep 27, 2016
Messages
37
Hi all,

I'm hoping what I'm after is quite straight forward.

I have a userform that has a TextBox and a command button on it.
When the userform loads up, it runs a small bit of code from a module that pulls in text from a text file stored on my C drive into the TextBox.

What I'd then like to do, is perform a search on the text that's just been pulled into the TextBox for key words and count how many times those words appear, with the results being in a message box or a label.

It's the search and count that I'm struggling with as I'm not sure where to begin with it.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
perhaps something along the lines of this
Code:
Sub Count_KeyWords()
    Dim str As String
    Dim arr As Variant
    Dim i As Long, counter as Long
    Dim rng As Range, cel As Range
str = textbox1.Text
arr = Split(str)
rng = Sheet1.Range("A1:A10") 'the range holding your list of key words
For Each cel In rng
    For i = LBound(arr) To UBound(arr)
        If arr(i) = cel.Value Then
            counter = counter + 1
        End If
    Next i
    MsgBox "The count for Key word " & cel.Value & " is " & counter
    counter = 0
Next cel
End Sub
 
Upvote 0
perhaps something along the lines of this
Code:
 Sub Count_KeyWords()
     Dim str As String
     Dim arr As Variant
     Dim i As Long, counter as Long
     Dim rng As Range, cel As Range
 str = textbox1.Text
 arr = Split(str)
 rng = Sheet1.Range("A1:A10") 'the range holding your list of key words
 For Each cel In rng
     For i = LBound(arr) To UBound(arr)
         If arr(i) = cel.Value Then
             counter = counter + 1
         End If
     Next i
     MsgBox "The count for Key word " & cel.Value & " is " & counter
     counter = 0
 Next cel
 End Sub


Thank you for the response, I'm just testing it now and I get a Run Time Error 91 - Object variable or With block variable not set on the line:

Code:
rng = Sheet1.Range("A1:A10") 'the range holding your list of key words

Not sure what that means or how to correct it.
 
Last edited:
Upvote 0
Put "Set" in front of it.

Also make sure the codename of the sheet you want your keywords in is indeed "Sheet1"...or you can use the tab name like this
Code:
Sheets("TabName").Range("A1:A10")
 
Upvote 0
Ah, that worked! Perfect, thank you!

One last question, instead of having the results pop up on a msgbox, what would I be looking to do to have them each in their own label instead?

So If I have a maximum of 10 keywords, I can list them down the side of the userform with the count next to them instead of the msgbox.
 
Upvote 0
Maybe something along these lines:

Code:
For Each cel In rng
For k = 1 to 10
For i = LBound(arr) To UBound(arr)
         If arr(i) = cel.Value Then
             counter = counter + 1
         End If
     Next i
     Userform1.Controls("Label" & k).Caption = counter
next k
     counter = 0
Next cel

or something similar, depending on how you name your labels. In this case the names would be "Label1", "Label2", "Label3", etc.
 
Last edited:
Upvote 0
Maybe something along these lines:

Code:
For Each cel In rng
For k = 1 to 10
For i = LBound(arr) To UBound(arr)
         If arr(i) = cel.Value Then
             counter = counter + 1
         End If
     Next i
     Userform1.Controls("Label" & k).Caption = counter
next k
     counter = 0
Next cel

or something similar, depending on how you name your labels. In this case the names would be "Label1", "Label2", "Label3", etc.

Apologies for the delay in replying.

Thank you for this, I will give it a go and see what happens.
 
Upvote 0

Forum statistics

Threads
1,215,040
Messages
6,122,806
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top