kelly mort
Well-known Member
- Joined
- Apr 10, 2017
- Messages
- 2,169
- Office Version
- 2016
- Platform
- Windows
Code:
Private Sub CommandButton1_Click()
Dim arr(1 To 2) As Variant, word As Variant
If Len(Me.TextBox1.Text) And Len(Me.TextBox2.Text) Then
arr(1) = Split(Me.TextBox1, " ")
arr(2) = Split(Me.TextBox2, " ")
For Each word In arr(1)
If IsError(Application.Match(word, arr(2), 0)) Then MsgBox "words do not match", 16, "No Match": Exit Sub
Next word
MsgBox "words Match"
End If
End Sub
I was trying to find out if the individual words exist in the two textboxes .
"Small Yellow Car" , "Car Small Yellow", " Yellow Car Small" , etc are all to give me "words match"
This code was provided to me here by @dmt32.
But I overlooked certain possibility at the start.
I only tested it with all boxes having 3 words. So I just decided today to dig deeper and saw a few setbacks. I used the algorithm this way:
Code:
Private Sub CmdTest_Click()
Dim arr(1 To 2) As Variant, word As Variant, dName As Variant, wStatus&
wStatus = 0
If Len(Trim(TextBox1)) Then
arr(1) = Split(Trim(TextBox1), " ")
For Each dName In Sheet1.[B2:B20]
If Len(Trim(TextBox1)) = Len(Trim(dName)) Then
arr(2) = Split(Trim(dName), " ")
For Each word In arr(1)
If Not IsError(Application.Match(word, arr(2), 0)) Then
wStatus = 1
Exit For
End If
Next word
If wStatus = 1 Then
MsgBox "Words match"
Exit For
ElseIf wStatus = 0 Then
MsgBox "Words do not match"
Exit For
End If
End If
Next dName
End If
End Sub
I found out that there are so many traps in my code:
In that column B, I want to compare each cell with textbox1 value for the possible words as shown above.
Is there "Small Yellow Car" in a cell in column B, and say "Car Yellow Small" or any of the combination in textbox1?
Then we shoot the right alert. My brain capacity is limited . I wish someone can help me with this.
I can see I am doing so many things wrongly yet I have no idea how to fix it.
I also observed that when there are variable words on the two textboxes , say two in box 1 and three words box 2, no alert is shown at all.