Reputation: 14537
I have an UserForm with 5 TextBoxes, and each time one is updated I recalculate the others.
But how do I avoid a "infinite loop" as the first will recalculate second which will recalculate the first and so on...
Here is a part of my UserForm's code, only the TextBox_Change(s) :
Private Sub TxtNewVal_Change()
If InStr(1, Me.TxtNewVal.Value, ",") Then Me.TxtNewVal.Value = Val(Replace(Me.TxtNewVal.Value, ",", "."))
'recalculate values
If Right(Me.TxtNewVal.Value, 1) <> "." Then
Me.TxtValEUR.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxEUR.Value)
Me.TxtValUSD.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxUSD.Value)
End If
End Sub
Private Sub TxtTxEUR_Change()
If InStr(1, Me.TxtTxEUR.Value, ",") Then Me.TxtTxEUR.Value = Val(Replace(Me.TxtTxEUR.Value, ",", "."))
If Val(Me.TxtTxEUR.Value) = 0 Then Me.TxtTxEUR.Value = TxEur
'recalculate values =
If Right(Me.TxtTxEUR.Value, 1) <> "." Then Me.TxtValEUR.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxEUR.Value)
'Me.TxtValUSD.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxUSD.Value)
End Sub
Private Sub TxtTxUSD_Change()
If InStr(1, Me.TxtTxUSD.Value, ",") Then Me.TxtTxUSD.Value = Val(Replace(Me.TxtTxUSD.Value, ",", "."))
If Val(Me.TxtTxUSD.Value) = 0 Then Me.TxtTxUSD.Value = TxUsd
'recalculate values =
'Me.TxtValEUR.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxEUR.Value)
If Right(Me.TxtTxUSD.Value, 1) <> "." Then Me.TxtValUSD.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxUSD.Value)
End Sub
Private Sub TxtValEUR_Change()
If InStr(1, Me.TxtValEUR.Value, ",") Then Me.TxtValEUR.Value = Val(Replace(Me.TxtValEUR.Value, ",", "."))
'recalculate values =
'Me.TxtValEUR.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxEUR.Value)
If Val(Me.TxtTxEUR.Value) <> 0 And Right(Me.TxtValEUR.Value, 1) <> "." Then _
Me.TxtNewVal.Value = Val(Me.TxtValEUR.Value) / Val(Me.TxtTxEUR.Value)
'Me.TxtValUSD.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxUSD.Value)
End Sub
Private Sub TxtValUSD_Change()
If InStr(1, Me.TxtValUSD.Value, ",") Then Me.TxtValUSD.Value = Val(Replace(Me.TxtValUSD.Value, ",", "."))
'recalculate values =
If Val(Me.TxtTxUSD.Value) <> 0 And Right(Me.TxtValUSD.Value, 1) <> "." Then _
Me.TxtNewVal.Value = Val(Me.TxtValUSD.Value) / Val(Me.TxtTxUSD.Value)
'Me.TxtValEUR.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxEUR.Value)
'Me.TxtValUSD.Value = Val(Me.TxtNewVal.Value) * Val(Me.TxtTxUSD.Value)
End Sub
Upvotes: 0
Views: 276
Reputation: 343
I'd use the Tag property of a control
Private Sub TxtNewVal_Change()
TxtNewVal.Value = Val(Replace(Me.TxtNewVal.Value, ",", "."))
'recalculate values
txtnewval.tag=" "
TxtValEUR.Value = Val(TxtNewVal) * Val(TxtTxEUR)
TxtValUSD.Value = Val(TxtNewVal) * Val(TxtTxUSD)
txtnewval.tag=""
End If
end sub
Private Sub TxtTxEUR_Change()
if txtnewval.tag=" " then exit sub
' - - - - -
end sub
Private Sub TxtTxUSD_Change()
if txtnewval.tag=" " then exit sub
' - - - - -
end sub
End Sub
Upvotes: 1
Reputation: 124696
Something like this:
Private bIsUpdating As Boolean
Private Sub TxtNewVal_Change()
If bIsUpdating Then Exit Sub
bIsUpdating = True
... update other text boxes
bIsUpdating = False
End Sub
Private Sub TxtValEUR_Change()
If bIsUpdating Then Exit Sub
bIsUpdating = True
... update other text boxes
bIsUpdating = False
End Sub
... etc ...
Upvotes: 1