+ Reply to Thread
Results 1 to 5 of 5

Error Prevention Application.InputBox

Hybrid View

AccountantCost Error Prevention... 11-07-2009, 05:08 PM
DonkeyOte Re: Error Prevention... 11-07-2009, 05:20 PM
AccountantCost Re: Error Prevention... 11-07-2009, 05:54 PM
DonkeyOte Re: Error Prevention... 11-07-2009, 06:00 PM
AccountantCost Re: Error Prevention... 11-07-2009, 06:43 PM
  1. #1
    Registered User
    Join Date
    05-28-2008
    Posts
    49

    Error Prevention Application.InputBox

    I am trying to add error prevention to my macro.

    Sub DebugMacro()
    '
    ' DebugMacro Macro
    ' Macro recorded 11/7/2009 by
    '
    
    Dim NumberofUniqueStencils As Integer
    Dim StencilMaterial(14) As String
    Dim StencilQuantity(14) As Variant
    Dim NumberofStencilLines() As Variant
    Dim CharactersPerLine(9) As Variant
    Dim CharacterHeight(9) As Variant
    Dim SymbolQuantity(9) As Variant
    Dim SymbolWidth(9) As Variant
    Dim GraphicWidth(9) As Variant
    Dim SymbolHeight(9) As Variant
    Dim TextLineSpacing(8) As Variant
    
    
    Dim Default As Integer
    
            On Error GoTo Canceled:
            prompt = "How many unique stencils is the customer ordering?"
            Caption = "Customer Order Information"
            Default = 1
            NumberofUniqueStencils = Application.InputBox(prompt, Caption, Default, Type:=1)
            If NumberofUniqueStencils = 0 Then
                Exit Sub
            End If
    
            
    
        For x = 1 To NumberofUniqueStencils
           
            On Error GoTo Canceled:
            prompt = "Input the material to be used for unique stencil number " & x & " ('OB', 'Mylar' or 'Mag')"
            Caption = "Customer Order Information"
            StencilMaterial(x) = Application.InputBox(prompt, Caption, Type:=2)        
        
            On Error GoTo Canceled:
            prompt = "Input the customer order quantity for unique stencil number " & x
            Caption = "Customer Order Information"
            Default = 1
            StencilQuantity(x) = (Application.InputBox(prompt, Caption, Default, Type:=1))
            If StencilQuantity(x) = 0 Then
                Exit Sub
            End If
        
            On Error GoTo Canceled:
            prompt = "How many text lines will unique stencil number " & x & " have?"
            Caption = "Customer Order Information"
            Default = 1
            ReDim NumberofStencilLines(Val(Application.InputBox(prompt, Caption, Default, Type:=1)) - 1)
            
            
        Next x
    
    Canceled: Exit Sub
    
    '
    End Sub
    In all dialog box prompts for user input, if a character is input when the a numberic is requested or the user selects the Cancel button, the macro exits.

    This works just as intended. However, for the code in red, if the user inputs a numeric instead of a character, the type:=2 critieria is ignored, the macro accepts the input (when it should not), and moves on to the next dialog box. Additionally, if the user selects the Cancel button, the macro moves on to the next dialog box instead of exiting. What additional code is needed to prevent the macro from proceeding to the next dialog box?

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Error Prevention Application.InputBox

    Given the variable is defined as a String you could check (post InputBox) both:

    a) Val(variable)

    b) variable = "False"

    By the sounds of it either of the above should ever return True the response is not valid.

  3. #3
    Registered User
    Join Date
    05-28-2008
    Posts
    49

    Re: Error Prevention Application.InputBox

    Ok, I think I see what you are saying - since the variable is defined as a string, a numeric value entered into a string defined variable will be treated as a string. Thus, the macro accepts the input and proceeds.

    If I followed correctly, then I propose the following:

     prompt = "Input the material to be used for unique stencil number " & x & " ('OB', 'Mylar' or 'Mag')"
            Caption = "Customer Order Information"
            StencilMaterial(x) = Application.InputBox(prompt, Caption, Type:=2)
            If IsNumeric(StencilMaterial(x)) Then
                Application.DisplayAlerts = True
                Resume
            End If
    The problem I am having with this approach is that if the input happens to be a value, the macro exits the sub. I thought by adding:

    Application.DisplayAlerts = True
    Resume
    that the macro would return to the section of the code causing the problem until the input is corrected.

    However, this is not happening. The macro just proceeds.

    What do I need to do?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Error Prevention Application.InputBox

    If you insist a repeat of the loop you obviously restrict flexibility but in basic terms

    ResumeCO:
    prompt = "Input the material to be used for unique stencil number " & x & " ('OB', 'Mylar' or 'Mag')"
    Caption = "Customer Order Information"
    StencilMaterial(x) = Application.InputBox(prompt, Caption, Type:=2)
    If Val(StencilMaterial(x)) Or StencilMaterial(x) = "False" Then GoTo ResumeCO

  5. #5
    Registered User
    Join Date
    05-28-2008
    Posts
    49

    Re: Error Prevention Application.InputBox

    Thanks for the feedback.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1