TextBox Change problem

mortgageman

Well-known Member
Joined
Jun 30, 2005
Messages
2,015
I am having the following problem with a textbox in my userform. The moment that any number is entered, the program starts. This is a problem if the user needs to input a 2 digit number - since the program kicks in as soon as the first digit is entered. :(

Thanks in advance for any help. Here is the code that I am using.

Gene, "The Mortgage Man", Klein


Private Sub UserGCFTextBox_Change()
Dim num1 As Integer, num2 As Integer, correctgcd As Integer
num1 = Val(TextBoxnumber1.Value)
num2 = Val(TextBoxnumber2.Value)
correctgcd = GCD(num1, num2)
If Val(UserGCFTextBox.Value) = correctgcd Then
RightGCFTextBox.Visible = True
RightGCFTextBox.Value = correctgcd
RightGCFTextBox.BackColor = vbGreen
Else
RightGCFTextBox.Visible = True
RightGCFTextBox.Value = correctgcd
RightGCFTextBox.BackColor = vbRed
End If
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Gene

What are youy actually trying to do?

Why not use another event, say Exit?
 
Upvote 0
What are youy actually trying to do?

Why not use another event, say Exit?
_________________

I don't know what you mean by Exit? The user (my son) puts in an answer for the GCF of two numbers. If it is right, a box comes up green, otherwise red. The problem is if the answer is more than one digit. The change macro kicks in the moment a digit is entered in the box.

Gene, "The Mortgage Man", Klein
 
Upvote 0
Well you are currently using the Change event, for something I'm assuming is data validation.

So instead of that use the textbox's Exit event to do that validation.

Of the top of my head I think the default event for a textbox is the change event. Therefore when you double click one in design view you end up with something like this.
Code:
Private Sub TextBox1_Change()

End Sub
Now the textbox has various other events, like Exit.

To access them use the right dropdown.
 
Upvote 0
How would the textbox know that an "Exit" has occured? Or in this case, how would the textbox know that the user has finished typing? Would s/he have to hit enter or something?

Gene, "The Mortgage Man", Klein
 
Upvote 0
Gene

The Exit event would be triggered by moving to any of the other controls on the form.

And in the exit event you can do your validation and if there is a problem stop focus going to the other control and if required display some message saying what the problem is.

Do you have any other controls?
 
Upvote 0
The Exit event would be triggered by moving to any of the other controls on the form.

And in the exit event you can do your validation and if there is a problem stop focus going to the other control and if required display some message saying what the problem is.

Do you have any other controls?
_________________
What happened to my stuff?

I guess this means that the user can't just type in an answer (unless the answer will always be one digit - which it wont) and expect something to happen. I will need (I assume) to add a "check my answer" button for this to work.

Gene, "The Mortgage Man", Klein
 
Upvote 0
You can avoid the need to click on a confirm button by just adding another textbox to your UserForm (this will provide another tab stop for the focus to switch to when you hit the enter key and trigger the Exit event).

You can hide the 2nd TextBox by making your Userform much taller at design time and placing the TextBox near the bottom of the form. Then resize the UserForm to something smaller so that the 2nd TextBox is not visible anymore.

When you run Form, you won't see the 2nd TextBox, but when you hit the Enter key after entering a 1, 2 or more digit number, the Exit event will occur and you can fire off whatever code you want to run.

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    
    MsgBox (TextBox1.Value)

End Sub
 
Upvote 0
I've went with the confirm button, but I'll keep it in mind for the next time.

Gene, "The Mortgage Man", Klein
 
Upvote 0

Forum statistics

Threads
1,214,387
Messages
6,119,208
Members
448,874
Latest member
Lancelots

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