我需要将三个文本框值格式化为货币。从电子表格中读取的初始值已在表格中格式化为货币,但是,我无法正确格式化文本框。如果该值不是整数,则该框将显示10.5.0而不是10.50。
这是我的项目 https://dl.dropboxusercontent.com/u/183889559/concession.xlsm
这是包含格式参数
的代码'enter user ID
Public Sub mavid_btn_Click()
mavID = Me.id_txt.Text 'get user id number entered
Dim i As Integer
i = 1
Sheets("TEAM ROSTER").Activate 'activate sheet with roster
Do While Cells(i, 1).Value <> "" 'while cell is not blank
If Cells(i, 1).Value = mavID Then 'if user input matches ID in team roster
name = Cells(i, 2).Value 'get cell value for name
Me.name_txt.Text = name 'populate name to name text box
balance = Cells(i, 3).Value 'get cell value for balance
Me.bal_txt.Text = VAL(balance) 'populate balance to balance text box
Me.bal_txt.Text = Format(Me.bal_txt.Text, "##.##")'
Dim history() As String
history = Split(Cells(i, 4).Value, ", ") 'split purchase history values in column D into array
Dim index As Variant
For Each index In history 'populate array to listbox
With Me.history_lbx
.AddItem index
End With
Next index
total = 0 'set total equal to 0 to start
payment = 0 'set payment equal to 0 to start
Me.total_txt.Text = VAL(total) 'set total to value
Me.pay_txt.Text = VAL(payment) 'set payment to value
balance = VAL(balance) ' set value
total = VAL(total) 'set total to value
payment = VAL(payment) 'set payment to value
Me.total_txt.Font.name = "Arial" 'text box formatting to match rest of form
Me.total_txt.Font.Size = 14
Me.pay_txt.Font.name = "Arial"
Me.pay_txt.Font.Size = 14
Exit Do
Else
i = i + 1 'else move to next cell in column A
End If
Loop
If balance < 0 Then 'formatting for balance based on sign (+ or -)
Me.bal_txt.ForeColor = vbRed
Else
Me.bal_txt.ForeColor = vbBlack
End If
End Sub
答案 0 :(得分:1)
使用您的代码,价值10.50美元。 Balance字段显示为&#34; $ 10.5&#34;不是&#34; 10.5.0&#34;如你所说。获得正确小数位的解决方案是更改以下行
Me.bal_txt.Text = Format(Me.bal_txt.Text, "##.##")
到
Me.bal_txt.Text = Format(Me.bal_txt.Text, "##.00")
此外,请检查编译错误,因为您不正确地引用了数组。