[英]VBA decimal places program
我需要一个程序,要求用户提供一个不超过 2 个小数位的数字,如果给出一个超过 2 个小数位的数字,则循环返回并再次询问。 然后能够输出带有相应小数位的数字。 这是我到目前为止...
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
而不是Inputbox
使用Application.InputBox与Type:=1
。 这将确保只有数字条目。
这是你正在尝试的吗? 这将允许像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.