Compare two variables from two different events

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
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