有没有更好的方法来评估文本框值
有没有更好的方法来简化此代码If IsNumeric(txtLengthFt) Then
If txtLengthFt.Value Mod 1 = 0 Then
intL = CInt(txtLengthFt) * 12
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.Show
End If
If IsNumeric(txtLengthIn) Then
If txtLengthIn.Value Mod 1 = 0 Then
intL = intL + CInt(txtLengthIn)
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.Show
End If
我已经制定了逻辑来确定用户是否输入了数值,但我不知道这是否是最好、最有效的方法
**** Hidden Message ***** 您可以简单地将textbox限制为只允许数字,从而避免检查值是否是数字。
Private Sub txtLengthFt_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Else
KeyAscii = 0
End Select
End Sub
谢谢马特!
我也学到了一些东西 谢谢马特!
如果他们按下一个非数字,什么都不按,会怎么样?我从来没有使用过,所以我现在正在尝试,但我想我会问,看看是否有任何我应该知道的问题。
太好了!我从来不知道你能做到。
谢谢马特 既然你已经向我展示了这一点,我将向你展示你刚刚从
If IsNumeric(txtLengthFt) Then
If txtLengthFt.Value Mod 1 = 0 Then
intL = CInt(txtLengthFt) * 12
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtLengthFt.SetFocus
Me.txtLengthFt.SelStart = 0
Me.txtLengthFt.SelLength = Len(txtLengthFt)
Me.Show
End If
If IsNumeric(txtLengthIn) Then
If txtLengthIn.Value Mod 1 = 0 Then
intL = intL + CInt(txtLengthIn)
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtLengthFt.SetFocus
Me.txtLengthIn.SelStart = 0
Me.txtLengthIn.SelLength = Len(txtLengthFt)
Me.Show
End If
If IsNumeric(txtWidthFt) Then
If txtWidthFt.Value Mod 1 = 0 Then
intW = CInt(txtWidthFt) * 12
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtWidthFt.SetFocus
Me.txtWidthFt.SelStart = 0
Me.txtWidthFt.SelLength = Len(txtWidthFt)
Me.Show
End If
If IsNumeric(txtWidthIn) Then
If txtWidthIn.Value Mod 1 = 0 Then
intW = intW + CInt(txtWidthIn)
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtWidthFt.SetFocus
Me.txtWidthIn.SelStart = 0
Me.txtWidthIn.SelLength = Len(txtWidthFt)
Me.Show
End If
If IsNumeric(txtHeightFt) Then
If txtHeightFt.Value Mod 1 = 0 Then
intH = CInt(txtHeightFt) * 12
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtHeightFt.SetFocus
Me.txtHeightFt.SelStart = 0
Me.txtHeightFt.SelLength = Len(txtHeightFt)
Me.Show
End If
If IsNumeric(txtHeightIn) Then
If txtHeightIn.Value Mod 1 = 0 Then
intH = intH + CInt(txtHeightIn)
Else
MsgBox "Enter a valid Length"
Me.Show
End If
Else
MsgBox "Enter a valid Length"
Me.txtHeightFt.SetFocus
Me.txtHeightIn.SelStart = 0
Me.txtHeightIn.SelLength = Len(txtHeightFt)
Me.Show
End If
中拯救了我 好吧,既然明白了,那我来问这个问题。3个文本框用于英寸。我在考虑使用CInt()转换成integer,并进行 12 Then
MsgBox "Whoa pardner!Nothing greater than 12 is allowed here!", vbCritical + vbOKOnly
' Enter a value of '0' in the textbox
TextBox1.Text = "0"
' Highlight the text in the textbox for immediate re-entry of number(s)
TextBox1.SelStart = 0
TextBox1.SelLength = TextBox1.TextLength
End If
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case Else
KeyAscii = 0
End Select
End Sub
看看为什么函数第二次循环,它不应该只是验证数据并继续前进。不要清除你的用户已经输入的数据,因为你搞砸了一个字段而不得不重新输入是愚蠢的。
如果验证了数据,则
blah blah
else
msgbox " You lush!这是不对的!”endif 我重新审视我正在做的事情。首先,我将所有的验证代码从按钮点击事件中移出,从而解决了这个问题。其次,我缩小了它循环的原因。这是因为我触发了事件,并且当我返回表单时没有退出click事件,所以事件在他们更新信息问题后再次运行。 好东西Matt
CmdrDuh-如果您的代码不在点击事件中,如何退出它?
页:
[1]
2