在 VBA 中格式化文本框 4 位小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7602512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:57:54  来源:igfitidea点击:

Formatting Textbox in VBA 4 place decimal

vbauserform

提问by Russell Saari

Is there anyway that you can format a textbox format to have four decimal places at all time? I know how to do it with C# and Visual Basic using a masked textbox, but vba is a bit more challeging due to the lack of function. Any help would be greatly appreciated. Thanks

无论如何,您可以将文本框格式设置为始终保留四位小数吗?我知道如何使用掩码文本框使用 C# 和 Visual Basic 来完成它,但由于缺乏功能,vba 更具挑战性。任何帮助将不胜感激。谢谢

Public Sub UserForm_Initialize()
TextBox6.Text = Format(Number, "###.####")
End Sub

回答by JohnFx

You just have the format string incorrect. It should look like this:

您只是格式字符串不正确。它应该是这样的:

Public Sub UserForm_Initialize()
    TextBox6.Text = Format(Number, "0.0000")
End Sub

回答by Reafidy

Try:

尝试:

Public Sub UserForm_Initialize()
     TextBox6.Text = Format(Number, "0.0000")
End Sub

Private Sub TextBox6_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If IsNumeric(TextBox6.Value) Then
        TextBox6.Text = Format(TextBox6, "0.0000")
    End If
End Sub

Private Sub TextBox6_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    '// Disregard keys other than 0-9/period/minus sign.
    If Shift Then KeyCode = 0
    Select Case KeyCode
    Case 8, 13, 46, 48 To 57, 96 To 105, 109, 110, 189, 190
    Case Else
        KeyCode = 0
    End Select
End Sub