Search for words in userform textbox

JeremySun

Board Regular
Joined
Jul 1, 2011
Messages
98
Is there a way to write a VBA that will search a Userform text box for several words, then if a word from the list is in the textbox a message will open up? I have no idea on how to code this. Thank you in advance.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Is there a way to write a VBA that will search a Userform text box for several words, then if a word from the list is in the textbox a message will open up? I have no idea on how to code this. Thank you in advance.

You could add something like this to a button click on your form ( Change the WORD_SOUGHT Const to suit )

Code:
    Const WORD_SOUGHT As String = "hello"
    Dim Spos As Long
    
    Spos = InStr(1, TextBox1.Text, WORD_SOUGHT, vbTextCompare)
    If Spos > 0 Then
        With TextBox1
            .SetFocus
            .SelStart = Spos - 1
            .SelLength = Len(WORD_SOUGHT)
        End With
        MsgBox "the word : '" & WORD_SOUGHT & "' was found."
    End If
 
Upvote 0
I would like to use VBTextcompare method to search the list of words that will be in column AA, then if the word does not match a word in the column I would like it to copy the textbox1 into the clipboard.
 
Upvote 0
I would like to use VBTextcompare method to search the list of words that will be in column AA, then if the word does not match a word in the column I would like it to copy the textbox1 into the clipboard.

Add a commandbutton to your worksheet or userform and add the following code to it :

Code:
Private Sub CommandButton1_Click()

    Dim oTargetRange As Range
    Dim oDataObject As Object
    
    On Error Resume Next
        Set oTargetRange = Me.Columns("A:A").Find _
        (What:=TextBox1.Text, After:=Cells(1))
    On Error GoTo 0
    
    If oTargetRange Is Nothing Then
        Set oDataObject = _
            GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        With oDataObject
            .Clear
            .SetText TextBox1.Text
            .PutInClipboard
        End With
    End If
    
End Sub
 
Upvote 0
For whatever reason it does not like Me.Columns("A:A") it gives me an error:

Compile error:
Method or data member not found and the .Columns is highlighted in green.
 
Upvote 0
For whatever reason it does not like Me.Columns("A:A") it gives me an error:

Compile error:
Method or data member not found and the .Columns is highlighted in green.

Where is the textbox ?

If it is in a worksheet then the code must go in the Worksheet Module and if it is on a userform then the code must go in the Form Module.
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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