EXCEL VBA 在写入或按下 tab 时格式化文本框
EXCEL VBA format textbox while writing or when tab is pressed
所以我的代码几乎完成了,只是遗漏了一件事,用户将不得不引入 12 个数字,例如“123456789123”,结果是 123-456.789-123,我设法得到了格式打印到 excel sheet,但我想要一些可以立即给我该格式的东西,因为它更容易发现任何输入错误。例如,用户将 123456789123 放在文本框上,当跳转到另一个框时,它会格式化。可能吗?
Private Sub TextBox1_Change()
If Len(TextBox1.Text) = 3 And TextBox1.Text Like "###" = True Then
TextBox1.Text = TextBox1.Text & "-"
If Len(TextBox1.Text) = 7 And TextBox1.Text Like "###-###" = True Then
TextBox1.Text = TextBox1.Text & "."
If Len(TextBox1.Text) = 11 And TextBox1.Text Like "###-###.###" = True Then
TextBox1.Text = TextBox1.Text & "-"
If Len(TextBox1.Text) > 15 Then TextBox1.Text = Left(TextBox1.Text, 15)
End Sub
所以我的代码几乎完成了,只是遗漏了一件事,用户将不得不引入 12 个数字,例如“123456789123”,结果是 123-456.789-123,我设法得到了格式打印到 excel sheet,但我想要一些可以立即给我该格式的东西,因为它更容易发现任何输入错误。例如,用户将 123456789123 放在文本框上,当跳转到另一个框时,它会格式化。可能吗?
Private Sub TextBox1_Change()
If Len(TextBox1.Text) = 3 And TextBox1.Text Like "###" = True Then
TextBox1.Text = TextBox1.Text & "-"
If Len(TextBox1.Text) = 7 And TextBox1.Text Like "###-###" = True Then
TextBox1.Text = TextBox1.Text & "."
If Len(TextBox1.Text) = 11 And TextBox1.Text Like "###-###.###" = True Then
TextBox1.Text = TextBox1.Text & "-"
If Len(TextBox1.Text) > 15 Then TextBox1.Text = Left(TextBox1.Text, 15)
End Sub