对于新手来说,这可以被认为是一个稍微“高于平均水平”的问题,所以我将回答这个问题:)
VBA 没有您所谓的掩码文本框,您可以在其中将格式设置为#,##0.00. 你只能做一个接受密码的掩码文本框,但这完全是另一回事。
这是我很快想到的。希望这是你想要的吗?
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, "#,##0.00")
'~~> Re-position the cursor
If InStr(1, TextBox1.Text, ".") - 1 > 0 Then _
TextBox1.SelStart = InStr(1, TextBox1.Text, ".") - 1
End Sub
Run Code Online (Sandbox Code Playgroud)
您也可以通过包含此代码将其提升到更高的级别。这确保用户只键入数字。
'~~> Numeric Textbox with Decimal Check
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case vbKey0 To vbKey9, vbKeyBack, vbKeyClear, vbKeyDelete, _
vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
If KeyAscii = 46 Then If InStr(1, TextBox1.Text, ".") Then KeyAscii = 0
Case Else
KeyAscii = 0
Beep
End Select
End Sub
Run Code Online (Sandbox Code Playgroud)
在行动