2014-03-28 89 views
-1

我需要將三個文本框值設置爲貨幣格式。從電子表格中讀取的初始值已被格式化爲表格中的貨幣,但是,我無法正確格式化文本框。如果該值不是整數,則該框將顯示10.5.0而不是10.50。VBA Excel 2010格式的文本框值爲currecy

這裏是我的項目 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 

末次

+0

鏈接不起作用。如果你想要一個代碼審查使用:http://codereview.stackexchange.com/。請嘗試將您的問題減少到一個較小的集合並修復鏈接。 – bitoiu

+0

感謝您的鏈接。我改變了我的問題並修復了鏈接。 – webhead

+0

我正在看這個,但好奇..在子程序'done_btn_Click()'你有行'histReturn = histReturn&j'histReturn是一個數組。你沒有收到編譯錯誤?你想用這個做什麼?並檢查你的循環! –

回答

1

使用你的代碼的代碼,併爲$ 10.50值。 Balance字段顯示爲「$ 10.5」而不是「10.5.0」,正如您所提到的那樣。讓出適當的小數位的解決方案是以下行

Me.bal_txt.Text = Format(Me.bal_txt.Text, "##.##") 

改變

Me.bal_txt.Text = Format(Me.bal_txt.Text, "##.00") 

另外,請檢查的編譯錯誤,因爲你不恰當地引用數組。

+0

由於某種原因,餘額文本框直到點擊付款按鈕後才顯示餘額 – webhead

+0

請澄清。當你輸入ID並點擊'Enter'時,顯示餘額IS。你有沒有檢查編譯錯誤?您發佈的鏈接不會編譯,因爲它缺少'EndIf' –