我想格式化特定用户表单上的任何文本框,以特定格式显示数字 我使用类模块在任何更改文本框上触发它 然后根据文本框的名称,按照所需的格式对其进行格式化。 我一开始输入一些数字,就会发现一些疯狂的数字,与我输入的数字不同 一旦我取出: txtbox.Value =格式(txtbox.Value,“ ### ### ## 0”) 我恢复了正常,除了我不格式化文本框
我尝试了另一个用户窗体上的另一个文本框,但我仍然有同样的担忧
“类模块“文本框”
Public WithEvents txtbox As MSForms.TextBox
Public Property Set TextBox(ByVal t As MSForms.TextBox)
Set txtbox = t
End Property
Private Sub txtbox_change()
Dim n As Integer
Select Case Left(txtbox.Name, 3)
Case "EMP"
'n = Mid(txtbox.Name, 4, 1)
n = Val(Right(txtbox.Name, 2))
ActiveSheet.Cells(n + 40, "fk").Value = Val(txtbox.Value)
txtbox.Value = Format(txtbox.Value, "### ### ##0")
'update formula side for rej EBI
If n = 10 Then
fillform.Controls("EBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fu").Value, "0.0%")
Else
fillform.Controls("EBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fu").Value, "0.0%")
fillform.Controls("EBI" & (n - 1)).Value = Format(ActiveSheet.Cells((n - 1) + 40, "fu").Value, "0.0%")
End If
Case "FIL"
'n = Mid(txtbox.Name, 4, 1)
n = Val(Right(txtbox.Name, 2))
ActiveSheet.Cells(n + 40, "fL").Value = Val(txtbox.Value)
txtbox.Value = Format(txtbox.Value, "### ### ##0")
'update formula side (%REJ ebi, & % REJ fbi)
If n = 10 Then
fillform.Controls("EBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fu").Value, "0.0%")
fillform.Controls("FBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fV").Value, "0.0%")
Else
fillform.Controls("EBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fu").Value, "0.0%")
fillform.Controls("EBI" & (n - 1)).Value = Format(ActiveSheet.Cells((n - 1) + 40, "fu").Value, "0.0%")
fillform.Controls("FBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fV").Value, "0.0%")
fillform.Controls("FBI" & (n - 1)).Value = Format(ActiveSheet.Cells((n - 1) + 40, "fV").Value, "0.0%")
End If
Case "FUL"
'n = Mid(txtbox.Name, 4, 1)
n = Val(Right(txtbox.Name, 2))
ActiveSheet.Cells(n + 40, "fM").Value = Val(txtbox.Value)
txtbox.Value = Format(txtbox.Value, "### ### ##0")
'update formula side (DESVIO, & %REJ FBI)
If n = 10 Then
fillform.Controls("DES" & n).Value = Format(ActiveSheet.Cells(n + 40, "fT").Value, "0.0%")
fillform.Controls("FBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fV").Value, "0.0%")
Else
fillform.Controls("DES" & n).Value = Format(ActiveSheet.Cells(n + 40, "fT").Value, "0.0%")
fillform.Controls("DES" & (n - 1)).Value = Format(ActiveSheet.Cells((n - 1) + 40, "fT").Value, "0.0%")
fillform.Controls("FBI" & n).Value = Format(ActiveSheet.Cells(n + 40, "fV").Value, "0.0%")
fillform.Controls("FBI" & (n - 1)).Value = Format(ActiveSheet.Cells((n - 1) + 40, "fV").Value, "0.0%")
End If
'force to write that because of sub "Fill Data" which trigger those
field
Case "DES"
'n = Mid(txtbox.Name, 4, 1)
n = Val(Right(txtbox.Name, 2))
ActiveSheet.Cells(n + 40, "ft").Value = Val(txtbox.Value)
txtbox.Value = Format(txtbox.Value, "###")
Case Else
txtbox.Value = Format(txtbox.Value, "0.00%")
End Select
End Sub
' code on my user form filldata
Private myEventHandlers As Collection
Private Sub UserForm_Initialize()
Dim txtbox As mytextbox
Set myEventHandlers = New Collection
Dim c As Control
For Each c In Me.Controls
If TypeName(c) = "TextBox" Then
Set txtbox = New mytextbox
Set txtbox.TextBox = c
myEventHandlers.Add txtbox
End If
Next c
End Sub