TextBox Change Event: Recursing Problem

Fugacity

New Member
Joined
Nov 23, 2012
Messages
7
My program consists of two textboxes performing a farenheit to celsius conversion using the textbox change event. If I type, for instance, 2 in a textbox I get 1.9999. This is annoying because I can't type in large numbers like 212 because the value immediately changes to 1.9999. I think the reason it is happening because it is recursing. When I type in a value it gets calculated back and forth...back and forth by the two subs until it loses its value.

Code:
Private Sub txt_C_Change()
Dim C, F, txtF, txtC As Double
If IsNumeric(txt_C.Text) Then
txtC = txt_C.Text
F = 1.8 * txtC + 32
txt_F.Text = CStr(F)
Else: txt_F.Text = ""
txt_C.Text = ""
End If
End Sub


Private Sub txt_F_Change()
Dim C, F, txtF, txtC As Double
If IsNumeric(txt_F.Text) Then
txtF = txt_F.Text
C = (txtF - 32) / 1.8
txt_C.Text = CStr(C)
Else: txt_C.Text = ""
txt_F.Text = ""
End If
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Welcome to MrExcel!

I'd use the Round() function on the answer where ever you might have the trouble:
Code:
TheNum = Round(TheNum, 0) 'to a whole number
 
Upvote 0
My program consists of two textboxes performing a farenheit to celsius conversion using the textbox change event. If I type, for instance, 2 in a textbox I get 1.9999. This is annoying because I can't type in large numbers like 212 because the value immediately changes to 1.9999. I think the reason it is happening because it is recursing. When I type in a value it gets calculated back and forth...back and forth by the two subs until it loses its value.

Rich (BB code):
Private Sub txt_C_Change()
Dim C, F, txtF, txtC As Double
Application.EnableEvents = False
If IsNumeric(txt_C.Text) Then
txtC = txt_C.Text
F = 1.8 * txtC + 32
txt_F.Text = CStr(F)
Else: txt_F.Text = ""
txt_C.Text = ""
End If
Application.EnableEvents = True
End Sub


Private Sub txt_F_Change()
Dim C, F, txtF, txtC As Double
Application.EnableEvents = False
If IsNumeric(txt_F.Text) Then
txtF = txt_F.Text
C = (txtF - 32) / 1.8
txt_C.Text = CStr(C)
Else: txt_C.Text = ""
txt_F.Text = ""
End If
Application.EnableEvents = True
End Sub
Try adding the lines of code I show in red above and see if that makes your code run better.
 
Upvote 0
Thanks for your help. I fixed the code. It's not the prettiest thing, but it works. How do you step through the whole module? I tried stepping through the code, but it only does it for a sub at a time? I was using f8.

Here is my code:
Code:
Public EnableEvents As Boolean
Sub CtxtChange()
Me.EnableEvents = False
Call txt_C_Change
End Sub


Sub FtxtChange()
Me.EnableEvents = False
Call txt_F_Change
End Sub
Private Sub txt_C_Change()
If Me.EnableEvents Then
            Exit Sub
        End If
Me.EnableEvents = True
Dim C, F, txtF, txtC As Double
If IsNumeric(txt_C.Text) Then
txtC = txt_C.Text
F = 1.8 * txtC + 32
txt_F.Text = CStr(F)
Else: txt_F.Text = ""
txt_C.Text = ""
End If
Me.EnableEvents = False
End Sub


Private Sub txt_F_Change()
If Me.EnableEvents Then
            Exit Sub
        End If
Me.EnableEvents = True
Dim C, F, txtF, txtC As Double
If IsNumeric(txt_F.Text) Then
txtF = txt_F.Text
C = (txtF - 32) / 1.8
txt_C.Text = CStr(C)
Else: txt_C.Text = ""
txt_F.Text = ""
End If
Me.EnableEvents = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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