[英]Format while typing EXCEL/VBA
After some search i camed arround with Siddharth Rout code for formatting the textbox while typing , but for no apparently reason it gives me a bad format or na infinite one. 经过一番搜索后,我用Siddharth Rout代码来搜索文本,以便在键入时格式化文本框,但显然没有理由,它给了我不好的格式或无穷大的格式。
Dim CursorPosition As Long
Dim boolSkip As Boolean
Dim countCheck As Long
Private Sub TextBox1_Change()
'~~> This avoids refiring of the event
If boolSkip = True Then
boolSkip = False
Exit Sub
End If
'~~> Get current cursor postion
CursorPosition = TextBox1.SelStart
boolSkip = True
'~~> Format the text
TextBox1.Text = Format(TextBox1.Text, "###-###.###-###")
'~~> Re-position the cursor
If InStr(1, TextBox1.Text, ".") - 1 > 0 Then _
TextBox1.SelStart = InStr(1, TextBox1.Text, ".") - 1
End Sub
The ideia is that the user types something like "123456789852" , 12-numbers and it formats to "123-456.789-852" while typing as it's easier to spot any input mistake. 想法是用户键入类似12的数字“ 123456789852”,并且在键入时将其格式设置为“ 123-456.789-852”,因为这样更容易发现任何输入错误。
Kind Regards 亲切的问候
In case any other answer would fail you could try this very ugly solution: 万一其他答案失败,您可以尝试以下非常丑陋的解决方案:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.