【发布时间】:2019-08-21 07:01:39
【问题描述】:
我在文本框值的格式和检查条件方面遇到了一些问题
输入后的每个文本框都会自动格式化为百分比。
输入最后一个文本框时,会检查总和是否等于1。
我可以在第一个工作。但是,当以某种方式检查三的总和时,我无法将值格式化为 value/double。我从 3 个文本框中获得的值仍在字符串中。我尝试转换但没有成功。
请看看我的代码有没有问题
Private Sub Land_Prob1_AfterUpdate()
On Error Resume Next
If Form_Simulation.Land_Prob1 <= 100 Then
Form_Simulation.Land_Prob1.Value = Format(Form_Simulation.Land_Prob1.Value / 100, "Percent")
ElseIf Form_Simulation.Land_Prob1 > 100 Or Form_Simulation.Land_Prob1 < 0 Then
Form_Simulation.Land_Prob1 = "0.00%"
End If
End Sub
Private Sub Land_Prob2_AfterUpdate()
On Error Resume Next
If Form_Simulation.Land_Prob2 <= 100 Then
Form_Simulation.Land_Prob2.Value = Format(Form_Simulation.Land_Prob2.Value / 100, "Percent")
ElseIf Form_Simulation.Land_Prob2 > 100 Or Form_Simulation.Land_Prob2 < 0 Then
Form_Simulation.Land_Prob2 = "0.00%"
End If
End Sub
Private Sub Land_Prob3_AfterUpdate()
Dim x1, x2, x3
On Error Resume Next
If Form_Simulation.Land_Prob3 <= 100 Then
Form_Simulation.Land_Prob3.Value = Format(Form_Simulation.Land_Prob3.Value / 100, "Percent")
ElseIf Form_Simulation.Land_Prob3 > 100 Or Form_Simulation.Land_Prob3 < 0 Then
Form_Simulation.Land_Prob3 = "0.00%"
End If
'Check if total probability equal 100
If Form_Simulation.Land_Prob1.Value <> "" And Form_Simulation.Land_Prob2.Value <> "" Then
x1 = Form_Simulation.Land_Prob1.Value
x1 = CDbl(x1)
x2 = Form_Simulation.Land_Prob2.Value
x2 = CDbl(x2)
x3 = Form_Simulation.Land_Prob3.Value
x3 = CDbl(x3)
If x1 + x2 + x3 <> 1 Then
MsgBox "Total probability must be equal 100%. Please enter again"
Form_Simulation.Land_Prob3 = ""
End If
End If
End Sub
无论我如何尝试转换,该值仍然是字符串,因此我无法将它们汇总以进行条件检查
感谢您的阅读
【问题讨论】:
-
除了下面给出的答案之外,将
Format(Form_Simulation.Land_Prob1.Value / 100, "Percent")更改为Format(Val(Form_Simulation.Land_Prob1.Value) / 100, "0.00%")顺便说一句,您可能还想检查文本框中是否输入了“数字”而不是一些随机文本... -
@SiddharthRout:谢谢。您能否澄清最后一点“可能要检查“数字”是否......“我真的不明白你的意思。
-
如果你在
Land_Prob1中输入PTQuoc会发生什么?