VBA - 在无限计算循环中具有多个文本框的用户窗体
VBA - UserForm with multiples TextBoxes in infinite calculation loop
我有一个包含 5 个文本框的用户窗体,每次更新一个时我都会重新计算其他的。
但是 如何避免 "infinite loop",因为第一个会重新计算第二个,第二个会重新计算第一个,依此类推...
这是我的用户窗体代码的一部分,只有 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
像这样:
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 ...
我会使用控件的标签 属性
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
结束子
我有一个包含 5 个文本框的用户窗体,每次更新一个时我都会重新计算其他的。
但是 如何避免 "infinite loop",因为第一个会重新计算第二个,第二个会重新计算第一个,依此类推...
这是我的用户窗体代码的一部分,只有 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
像这样:
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 ...
我会使用控件的标签 属性
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
结束子