Excel VBA格式文本框在用户表单中显示正确的小数和百分比



我很难弄清楚如何在我的简单表单中正确格式化文本框。下面是我的代码:

Private Sub cmdCalculate_Click()
If IsNumeric(Me.txt_odds.value) Then
Me.txt_odds = Format(txt_odds.value, "0" & Application.DecimalSeparator & "00")
End If
Me.txt_Minimum = Format(100 / txt_odds.value, "0" & Application.DecimalSeparator & "00%")
End Sub

然而,我得到以下结果:

形象例如,如果我输入1.5,并期望它是1.50,我实际得到的是15.00。

期待您的帮助。谢谢!

为什么将txt_odds中的值除以100 ?

难道不应该反过来吗?

Private Sub cmdCalculate_Click()
If IsNumeric(Me.txt_odds.Value) Then
Me.txt_odds = Format(txt_odds.Value, "0" & Application.DecimalSeparator & "00")
End If
Me.txt_Minimum = Format(txt_odds.Value / 100, "0" & Application.DecimalSeparator & "00%")
End Sub

试试这个:

Private Sub cmdCalculate_Click()
Dim Odds    As Double
If IsNumeric(Me.txt_odds.Value) Then
Odds = Val(Me.txt_odds.value)
Me.txt_odds = Format(Odds, "0.00")
Me.txt_Minimum = Format(Odds, "0.00%")
End If    
End Sub

尝试使用,CDBL代替文本框的值,

Private Sub cmdCalculate_Click()
Dim Odds    As Double
on error resume next ' if cant convert will skip
Odds = cdbl(Me.txt_odds.text)
Me.txt_odds = Format(Odds, "0.00")
Me.txt_Minimum = Format(Odds, "0.00%")

End Sub

解析如下:

Dim vTR As String
vTR = Replace(Me.txt_odds.value, ".", ",")
Me.txt_odds = Format(vTR, "0.00")
If IsNumeric(Me.txt_odds.value) Then
Me.txt_Minimum = Format(100 / vTR, "0.00") & "%"
End If

相关内容

  • 没有找到相关文章

最新更新