Consulting

Results 1 to 5 of 5

Thread: VBA Inputs restricted to specific decimal ending values

  1. #1

    VBA Inputs restricted to specific decimal ending values

    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.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    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?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Thanks Paul! It works great. Marked as solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •