我正在创建一个用户表单来输入地址数据。我有一个用于地址名称的文本框,用于选择道路,街道等的组合框,以及用于奇数从和到达的4个数字框,从和到达均匀。
我遇到的问题是让VBA清除“to”框中的内容,如果已经输入了数字,但是用户将这些框从“框”更改为“to”框之后的更大值。
以下是我目前的代码:
Private Sub txtstreet_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 65 To 90
Case 97 To 122
Case 32
Case 127
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub UserForm_Initialize()
roaddesciptor.List = Array("ROAD", "STREET", "WAY", "AVENUE", "DRIVE", "GROVE", "LANE", "GARDENS", "PLACE", "CRESENT", "CLOSE", "SQUARE", "HILL", "CIRCUS", "MEWS", "VALE", "RISE", "ROW", "MEAD", "WHARF")
roaddesciptor.ListIndex = 0
End Sub
Private Sub txtaddoddfrom_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txtaddoddfrom_AfterUpdate()
If txtaddoddfrom.Value < txtaddoddto.Value Or txtaddoddto = "" And txtaddoddfrom.Value Mod 2 = 1 Then Exit Sub
If txtaddoddfrom.Value Mod 2 <> 1 Or txtaddoddfrom.Value > txtaddoddto.Value Then txtaddoddfrom = ""
'Else: txtaddoddfrom = ""
End Sub
Private Sub txtaddoddto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txtaddoddto_AfterUpdate()
If txtaddoddto.Value Mod 2 = 1 And txtaddoddto.Value >= txtaddoddfrom.Value Then Exit Sub
If txtaddoddto.Value Mod 2 <> 1 Or txtaddoddto.Value < txtaddoddfrom.Value Then txtaddoddto = ""
End Sub
Private Sub txtaddevenfrom_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txtaddevenfrom_AfterUpdate()
If txtaddevenfrom.Value < txtaddevento.Value Or txtaddevento = 0 And txtaddevenfrom.Value Mod 2 <> 1 Then Exit Sub
If txtaddevenfrom.Value Mod 2 = 1 Or txtaddevenfrom.Value > txtaddevento.Value Then txtaddevenfrom = ""
End Sub
Private Sub txtaddevento_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub txtaddevento_AfterUpdate()
If txtaddevento.Value Mod 2 <> 1 And txtaddevento.Value >= txtaddevenfrom.Value Then Exit Sub
If txtaddevento.Value Mod 2 = 1 Or txtaddevento.Value < txtaddevenfrom.Value Then txtaddevento = ""
End Sub
答案 0 :(得分:0)
为了澄清,您要将代码添加到“发件人”框的AfterUpdate事件中,如果新的“发件人”框编号大于“收件人”框编号,则会删除“收件人”框。
如果您将以下代码行添加到“发件人”框AfterUpdate事件中,它应该可以执行您想要的操作。它与您已有的相同,只是您要清除“发件人”框,而不是“收件人”框。
txtaddoddfrom.Value > txtaddoddto.Value Then txtaddoddto = ""
答案 1 :(得分:0)
这里有三个问题要处理。
首先,你不能在字符串上使用数字运算符,这就是空白字段。有许多不同的方法可以实现这一点。一种方法是(0 & "")
。另一种是明确检查它们。我在调整你的代码时使用了这两个。
其次,尝试更改您要使用Tab键的字段值失败。你必须围绕它成功的尝试包装Application.EnableEvents
语句。
最后,你正在和#34;字段而不是&#34;到&#34; &#34;来自&#34;中的字段处理程序。
这里修复了一对处理程序:
Private Sub txtaddoddfrom_AfterUpdate()
If (0 & txtaddoddfrom) Mod 2 <> 1 Then txtaddoddfrom = "" ' If even (or ""), zap it
If txtaddoddfrom = "" Or txtaddoddto = "" Then Exit Sub 'If was even or blank, done -> exit
Application.EnableEvents = False
If txtaddoddto < txtaddoddfrom Then txtaddoddto.Value = "" ' Otherwise compare and zap txtaddoddto if smaller
Application.EnableEvents = True
End Sub
Private Sub txtaddoddto_AfterUpdate()
If (0 & txtaddoddto) Mod 2 <> 1 Then txtaddoddto = "" ' If even (or ""), zap it
If txtaddoddto = "" Or txtaddoddfrom = "" Then Exit Sub 'If was even or blank, done -> exit
Application.EnableEvents = False
If txtaddoddfrom > txtaddoddto Then txtaddoddfrom = "" ' Otherwise compare and zap txtaddoddfrom if greater
Application.EnableEvents = True
End Sub
注意:可以通过显式使用.Value
来访问字段值,或者在没有它的情况下隐式使用字段值。始终使用一种形式或另一种形式是一个好主意。