[英]VBA decimal places program
I need a program that requires the user to provide a number with no more than 2 decimal places , and if a number with more than 2 decimals is given to loop back and ask again.我需要一个程序,要求用户提供一个不超过 2 个小数位的数字,如果给出一个超过 2 个小数位的数字,则循环返回并再次询问。 Then to be able to output that number with the corresponding decimal places.
然后能够输出带有相应小数位的数字。 This is what I have so far...
这是我到目前为止...
Sub program()
Dim num as Double
num = Inputbox("Give me a number with no more than 2 decimal places:")
MsgBox "Your time was " & num & " seconds."
End Sub
Instead of Inputbox
use Application.InputBox with Type:=1
.而不是
Inputbox
使用Application.InputBox与Type:=1
。 This will ensure only numeric entries.这将确保只有数字条目。
Is this what you are trying?这是你正在尝试的吗? This will allow values like
1,1.1,1.11,.1,.11
这将允许像
1,1.1,1.11,.1,.11
这样的值
Sub Sample()
Dim Ret
Dim digitsAfterDecimal As Integer
Do
Ret = Application.InputBox(prompt:="Enter a number with max 2 decimal places", Type:=1)
'~~> If user presses cancel
If Ret = False Then Exit Do
'~~> Number of digits after decimal
digitsAfterDecimal = Len(CStr(Ret)) - InStr(CStr(Ret), ".")
'~~> This will allow up to 2 decimal
If digitsAfterDecimal < 3 Then Exit Do
Loop
If Ret <> False Then MsgBox Ret
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.