PDA

View Full Version : [SOLVED] Input mask percentages



crender2000
10-12-2013, 12:03 PM
I know this is simple have found a few examples in VB not sure if it would be the same in VBA. The example I found in VB is "####" would this be correct in VBA? I am wanting to force the user to only be able to enter a percentage. It is very important the rest of the program will not work unless they enter a percentage. Must be 4 digits does not matter if it is a string I can convert it latter. Thanks in advance for your help.

Do
Do Until IsValid = True
'Ask the user to input percentage into input box
XPercentage = InputBox("Enter a Percentage", "Percentage Code",
If XPerecentage = "" Then
Exit Sub
End If

If (User did enter percentage) Then
IsValid = True
Else
MsgBox "You must enter Percentage", vbCritical, "Percentage"

End If
Loop

Paul_Hossler
10-12-2013, 03:47 PM
To be sure, how are you defining 'percentage'?

I'm guessing that you want a numeric value between 0.000 and 1.000

Or are you looking for a string = "55.44%" or something similar?

Paul

crender2000
10-12-2013, 06:31 PM
Yes I am wanting a numeric value between 0.000 and 1.000. However either way would work. The main thing is if the user types any thing but 4 digits and a decimal point they get an error message.

Paul_Hossler
10-13-2013, 05:51 AM
'Like' would probably do the masking ( = "0.000") (first example), but it might be more 'user friendly' (second example) to allow numbers from the user and the macro tests and formats. You said that "Must be 4 digits does Not matter If it Is a String I can convert it later" so why not just keep it as a number?




Option Explicit
Sub DemoAsAsked()
Dim XPercentage As String
Dim IsValid As Boolean

IsValid = False
Do While Not IsValid
'Ask the user to input percentage into input box
XPercentage = InputBox("Enter a Percentage (#.###)", "Percentage Code", vbNullString)
If Len(XPercentage) = 0 Then
Exit Sub
End If

If XPercentage Like "#.###" Then
IsValid = True
Else
MsgBox "You must enter Percentage (#.###)", vbCritical, "Percentage"
End If
Loop
MsgBox "You entered a valid 5 char string = " & XPercentage
End Sub
.
.
.
===================
.
.
.

Sub Suggested()
Dim XPercentage As Variant
Dim IsValid As Boolean

IsValid = False
Do While Not IsValid
'Ask the user to input percentage into input box
XPercentage = Application.InputBox("Enter a Percentage between and including 0 and 1" & _
vbCrLf & vbCrLf & "[Cancel to Exit]", "Percentage Code", vbNullString, , , , , 1)

If VarType(XPercentage) = vbBoolean Then
If Not XPercentage Then Exit Sub

ElseIf XPercentage < 0# Or XPercentage > 1# Then
MsgBox "You must enter Percentage between 0.0 and 1.0", vbCritical, "Percentage"

Else
XPercentage = Round(XPercentage, 3)
IsValid = True
End If
Loop
MsgBox "You entered a number " & XPercentage & " which can be formatted as " & Format(XPercentage, "0.000")
End Sub



Paul

crender2000
10-13-2013, 09:37 AM
Your first example is exactly what I am looking for. Thank you very much.