VBA code to compare contents of two textboxes

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. 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.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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