Compare two variables from two different events

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,799
Office Version
  1. 2016
Platform
  1. Windows
I am looking for a way to capture a textbox data with two variables then compare to see if they match or not.

That's, when I enter the textbox, I want to capture it's value.

Then when I Exit that textbox, I want to capture that value as well.

It may happen that I just enter and leave or I change its data.

And in each case I want the two variables set.

I was thinking of the "Enter" and "After Update" events . But I have no idea how to compare those two variables from the two separate subs.


I am talking about userform textbox here.

So a command button when clicked will display the alert:

"Match" or "Not Match"

Thanks in advance
 

Some videos you may like

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,026
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
Hi there.

Try this in your userform code:
Code:
Dim EnterValue As Variant
Dim ExitValue As Variant

Private Sub CommandButton1_Click()
If EnterValue = ExitValue Then
    MsgBox "Match"
Else
    MsgBox "No Match"
End If
End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub TextBox1_Enter()
EnterValue = Me.TextBox1.Value
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ExitValue = Me.TextBox1.Value
End Sub
 
Last edited:

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
1,799
Office Version
  1. 2016
Platform
  1. Windows
Hi there.

Try this in your userform code:
Code:
Dim EnterValue As Variant
Dim ExitValue As Variant

Private Sub CommandButton1_Click()
If EnterValue = ExitValue Then
    MsgBox "Match"
Else
    MsgBox "No Match"
End If
End Sub

Private Sub TextBox1_Change()

End Sub

Private Sub TextBox1_Enter()
EnterValue = Me.TextBox1.Value
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ExitValue = Me.TextBox1.Value
End Sub


Great !! !

Very brilliant

Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,109,549
Messages
5,529,470
Members
409,884
Latest member
Msinmath
Top