我有一个包含5个TextBox的UserForm,每次更新一个我都会重新计算其他文件。
但是我如何避免"无限循环" ,因为第一个将重新计算第二个将重新计算第一个等等...
以下是我的UserForm代码的一部分,只有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
答案 0 :(得分:1)
这样的事情:
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 ...
答案 1 :(得分:1)
我使用控件的Tag属性
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