VBA格式Userform数字输出

时间:2017-07-23 18:46:21

标签: excel vba excel-vba formatting userform

我有一个电子表格/用户形式组合,用于输入用户输入来计算产品定价/百分比税/平方英尺,以便在其中一个用户表单的文本框中输出地板的总成本。

我的userform正在正确计算所有内容,但我正在尝试弄清楚如何格式化输出框,以便它只显示小数点后最多两位数的值(即$ 1.00)。目前,它最多显示小数点后的四位数或更多(如“总面积”,“税额”和“最终价格”文本框中所示)。

enter image description here

我的用户形式代码如下(我省略了一些与打开和关闭用户窗体有关的非相关部分,但是那些与其功能有关的事情就在那里):

Public Sub SumTool()

  Dim A, B, C, D, E, F As Double
  Dim x As Double
  Dim finalSum As Double
  Dim addUp As Double
  Dim BeforePercent As Double
  Dim Prcnt As Double
  Dim percentALT As Double
  Dim percentSum As Double
  Dim i As Integer


  addUp = 0
  finalSum = 0
  BeforePercent = 0
  x = 0
  i = 0

'These are all area measurements

  A = 280
  B = 118
  C = 96
  D = 243
  E = 38
  F = 83

  Do While i < 1

'These are checks to see if checkboxes in the userform are True/False and 
'correspond to the area measurements above

    If LR.Value = True Then        
     x = x + A   
    Else
     x = x
    End If

   If BR1.Value = True Then
    x = x + B
   Else
    x = x
   End If

  If BR2.Value = True Then
   x = x + C
  Else
   x = x
  End If

  If KT.Value = True Then
   x = x + D
  Else
   x = x
  End If

 If BA.Value = True Then
  x = x + E
 Else
  x = x
 End If

 If HALL.Value = True Then
  x = x + F
 Else
  x = x
 End If

 i = i + 1

 Loop

'I have different calculations because the user has the option of 
'whether they want to include tax or not. If they do not (first option)
'no special conversions have to take place. If they do, the program has to
'take the entry and convert it from 5 or 10 to 0.05 or 0.10 and then carry
'forward with the rest of the operations

   If Me.Y.Value = False Then

     Prcnt = 0
     addUp = x
     finalSum = addUp * Me.ProductPrice.Value
     Me.FinalResultsBox.Value = finalSum
     Me.SqFtBox.Value = addUp
     Me.TaxAmountValue.Value = 0

   Else

     Prcnt = Me.SalesTaxNumber.Value
     addUp = x
     percentALT = Prcnt * 0.01
     BeforePercent = addUp * Me.ProductPrice.Value
     percentSum = percentALT * BeforePercent
     finalSum = BeforePercent + percentSum
     Me.FinalResultsBox.Value = finalSum
     Me.SqFtBox.Value = addUp
     Me.TaxAmountValue.Value = percentSum

   End If

End Sub

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情......

Me.FinalResultsBox.Value = Format(finalSum, "$0.00")