跳过特殊字符一次input2个字符的VBA用户表单

不知道是否有可能,但是有一段代码说明当你input2个字符,然后跳过:或/符号(一个date和一个需要手动input的时间。

我猜测它需要在文本框的变化事件,但不知道在input一次2焦点设置为3位数的代码types。

谢谢Al

假设您有一个带有2个文本框的示例UserForm1(date为TextBox1 ,时间为TextBox2

在这里输入图像描述

您可以不断检查文本框中键入的string的长度,如果它等于date的8,或者您操作值的时间是4。

 Private Sub TextBox1_Change() Dim current As String current = TextBox1.Value If Len(TextBox1) = 8 Then current = Left(TextBox1, 2) & "/" & Mid(TextBox1, 3, 2) & "/" & Right(TextBox1, 4) TextBox1 = current End If End Sub Private Sub TextBox2_Change() Dim current As String current = TextBox2.Value If Len(TextBox2) = 4 Then current = Left(TextBox2, 2) & ":" & Right(TextBox2, 2) TextBox2 = current End If End Sub 

所以现在如果您启动UserForm1并input例如10102014 ,代码将自动将其转换为date格式,并在字符之间添加正斜杠。 同样的时间

在这里输入图像描述