Consulting

Results 1 to 3 of 3

Thread: Data Validation in a MessageBox - VBA

  1. #1

    Data Validation in a MessageBox - VBA

    Hi there,

    In Excel VBA, I have a message box that must only accept negative, odd, double digit numbers, and then sum all numbers from the inputted number to zero. I have taken a portion of code from online and have been trying to modify it to my situation. So far, I am able to get the negative portion to work, so the input only accepts negative numbers. I have also been able to get even numbers on with Mod 2 = 0, however Mod 2 = 1 does not change it is odd numbers only. As well, I have no idea how to go about having the message box only accept double digit numbers. I think it has something to do with Len(2) but I am unsure how to implement it.

    I have attached my workbook below. Any help is greatly appreciated.

    Negative Odd Double Digit.xlsm

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I'd do something like this

    I prefer to have 'byte-size' value tests

    I also added (at least for my testing) an 'escape' value of 0


    Option Explicit
    
    Sub NegativeOddInteger()
        Dim Sum As Double
        Dim NumberInput As Variant
        Dim x As Double
        Dim NumberOK As Boolean
        
        NumberOK = False
        
        On Error GoTo BadInput
        
        Do
            
    GetNumber:
            NumberInput = InputBox("Please Enter a Negative Odd Double Digit Integer, '0' to Exit")
            
            If NumberInput = 0 Then Exit Sub
            
            If Not IsNumeric(NumberInput) Then Err.Raise 1000, "NegativeOddInteger", "Not a number"
            If NumberInput >= 0 Then Err.Raise 1001, "NegativeOddInteger", "Not negative"
            If Abs(Int(NumberInput)) <> Abs(NumberInput) Then Err.Raise 1002, "NegativeOddInteger", "Not an integer"
            If Abs(NumberInput) Mod 2 <> 1 Then Err.Raise 1003, "NegativeOddInteger", "Not odd"
            If NumberInput < -99 Or NumberInput > -10 Then Err.Raise 1004, "NegativeOddInteger", "Not 2 digits"
            
            NumberOK = True
        
        Loop Until NumberOK
            
        For x = NumberInput To 0 Step 2 'This sums numbers from input to zero, doing odds only.
            Sum = Sum + x
        Next
            
        MsgBox ("This equals " & Sum) & vbCrLf & ("based on the inputted number of ") & NumberInput
        
        Exit Sub
            
    BadInput:
            MsgBox Err.Description & " (" & Err.Number & ") Try again"
            Resume GetNumber
    End Sub
    Last edited by Paul_Hossler; 10-25-2018 at 07:17 AM. Reason: Fiddle Formatting a bit
    ---------------------------------------------------------------------------------------------------------------------

    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
    Thank you so much! The code worked perfectly.

Posting Permissions

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