Input Box Q

S

Seanie

I have a very simple Input Box as below, which asks a user to enter a
value. How can I build in validations within it that will check to
ensure that the value entered is a percentage, between 0% and 99%.
Also can I trap that the user must enter something and is not allowed
just to cancel out of it? My input box below has a OK/Cancel button

Sub test01()
UserValue = InputBox("Value to use?")
Range("B3").Value = UserValue

End Sub
 
R

Roger Govier

Hi

Something like

Sub test01()
uservalue = InputBox("Value to use? Between 0% and 99%")
uservalue2 = Format(uservalue, "#.00")
If uservalue2 < 0 Or uservalue2 > 99 Then
MsgBox "Value outside range of 0% to 99%"
Exit Sub
End If
If uservalue2 > 1 Then
uservalue = Format(uservalue2 / 100, "#%")
Else
uservalue = Format(uservalue2, "#%")
End If
Range("B3").Value = uservalue
End Sub
 
J

Jacob Skaria

Dear Seanie

Try this. You can include more validations ....

Sub test01()

Do
isvalidvalue = True
uservalue = InputBox("Value to use?")
'---Add all validations here and set variable IsValidValue to False if fails
If Trim(uservalue) = "" Then isvalidvalue = False
If CInt(uservalue) < 0 Or CInt(uservalue) > 99 Then isvalidvalue = False
'------------
Loop While isvalidvalue = False

Range("B3") = uservalue

End Sub

If this post helps click Yes
 
S

Seanie

Thanks Roger, I've tried to format the output cell as 0.00%, but it
only shows as 0.00, why is that?

uservalue = InputBox("Value to use? Between 0% and 99%")
uservalue2 = Format(uservalue, "#.00")
If uservalue2 < 0 Or uservalue2 > 99 Then
MsgBox "Value outside range of 0% to 99%"
Exit Sub
End If
If uservalue2 > 1 Then
uservalue = Format(uservalue2 / 100, "#%")
Else
uservalue = Format(uservalue2, "#.00%")
End If
Range("B3").Value = uservalue
Selection.NumberFormat = "0.00%"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top