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

时间:2011-09-29 19:38:08

标签: vba userform

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

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

2 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:1)

尝试:

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