假设我们在userform中有一个文本框,它将与frontslash" /"一起接收任何数字输入。但我想要输入的定义格式;如果用户试图输入任何不符合格式的字符,它应该是
a)不接受角色,或
b)接受并立即移除该角色(表明它不接受输入)。
我有这段代码,
Private Sub DateTextBox_Change()
With DateTextBox
If .value Like "#" Then Exit Sub
If .value Like "[0-1][0-9]" Then Exit Sub
If .value Like "#[/]" Then Exit Sub
If .value Like "[0-1][0-9][/]" Then Exit Sub
If .value Like "#[/][0-9]" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]" Then Exit Sub
If .value Like "#[/][0-9]#" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#" Then Exit Sub
If .value Like "#[/][3]" Then Exit Sub
If .value Like "[0-1][0-9][/][3]" Then Exit Sub
If .value Like "#[/][3][0-1]" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1]" Then Exit Sub
If .value Like "#[/][3][0-1][/]" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1][/]" Then Exit Sub
If .value Like "#[/][0-9]#[/]" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#[/]" Then Exit Sub
If .value Like "#[/][3][0-1][/]#" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1][/]#" Then Exit Sub
If .value Like "#[/][0-9]#[/]#" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#[/]#" Then Exit Sub
If .value Like "#[/][3][0-1][/]##" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1][/]##" Then Exit Sub
If .value Like "#[/][0-9]#[/]##" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#[/]##" Then Exit Sub
If .value Like "#[/][3][0-1][/]###" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1][/]###" Then Exit Sub
If .value Like "#[/][0-2]#[/]###" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#[/]###" Then Exit Sub
If .value Like "#[/][3][0-1][/]####" Then Exit Sub
If .value Like "[0-1][0-9][/][3][0-1][/]####" Then Exit Sub
If .value Like "#[/][0-2]#[/]####" Then Exit Sub
If .value Like "[0-1][0-9][/][0-2]#[/]####" Then Exit Sub
If Len(.value) = 0 Then Exit Sub
.value = Left(.value, Len(.value) - 1)
End With
End Sub
不接受日期02/9/2017
。它将仅接受02/
之前的字符。
提前谢谢!