PDA

View Full Version : [SOLVED:] VBA Inputs restricted to specific decimal ending values



dannywinters
10-25-2018, 06:25 PM
Good evening,

I have something that I hope is a very simple piece of code, but I have had no success figuring it out. What I need to do is have an input box or even a user form that only accepts ".55" at the end of inputted numbers. It must also only accept positive numbers, and no letters. So an example would be "55.55" or "94.55". I am doing this as the data I am working with, all the values end in .55, so the user inputs must be consistent with that also. Thank you in advance.

Paul_Hossler
10-25-2018, 07:08 PM
Maybe something like this




Option Explicit

Function GoodData(s As Variant) As Boolean


GoodData = False

If Not IsNumeric(s) Then Exit Function
If s <= 0 Then Exit Function
If Right(s, 3) <> ".55" Then Exit Function

GoodData = True
End Function

Sub test()
MsgBox GoodData("abcd")
MsgBox GoodData(-123)
MsgBox GoodData(11.99)
MsgBox GoodData(100.555)
MsgBox GoodData(100.55)
End Sub

dannywinters
10-25-2018, 07:52 PM
Hi Paul,

The function seems to be working. How would I incorporate an input box that would then use the GoodData function to test if their inputted value was correct?

Paul_Hossler
10-26-2018, 07:39 AM
Option Explicit


Function GoodData(s As Variant) As Boolean

GoodData = False

If Not IsNumeric(s) Then Exit Function
If s <= 0 Then Exit Function
If Right(s, 3) <> ".55" Then Exit Function

GoodData = True
End Function



Sub test2()
Dim v As Variant

v = Application.InputBox("Enter number > 0 ending in .55", "Input Number")
If GoodData(v) Then
MsgBox "I like it -- " & v
Else
MsgBox "I don't like it -- " & v
End If
End Sub

dannywinters
10-26-2018, 11:20 AM
Thanks Paul! It works great. Marked as solved.