Validation of texts from two different textboxes

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
Hello
I am looking for a script that will compare the values in two textboxes to see if the both contain same words.

One box has

"Small Yellow Car"

Second box

"Car Yellow Small"
"Small Car Yellow"

Etc

Rules :

1. The order does not matter
2. The length of characters in both cases must be equal
3. The case of the letters should not matter
4. The words are separated by a space.


Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi,
one idea maybe

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

Dave
 
Upvote 0
Hi,
one idea maybe

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

Dave

Sure!!!


It's doing the job greatly.

That Len function is confusing me.

Why did it still work when there is no operations made ?
 
Upvote 0
Sure!!!


It's doing the job greatly.

That Len function is confusing me.

Why did it still work when there is no operations made ?

I just included it to ensure both textboxes had some data otherwise possible to get a match if both are blank or type mismatch error if one left blank

Dave
 
Upvote 0
Okay I just learned something new.

I was looking forward to equal sign or the like.

I appreciate it
 
Upvote 0
One more thing:


Now that the textboxes are working great, I want to go a step further by replacing TextBox2 with column B on my worksheet.

So I want to look from B2 to Say B20 then verify if their content matches TextBox1 .

We are looking and comparing each cell in that range to the TextBox1 for a match as we did for the textbox.

I am thinking of using the "for each" loop but can't see the syntax correctly.

I will like to exit the for loop when my first match is found.
 
Upvote 0
This is how I have fixed that problem. I want the experts have a look for me if I am on course.


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
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,178
Members
452,615
Latest member
bogeys2birdies

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