Consulting

Results 1 to 2 of 2

Thread: Input Box and Msg box Help

  1. #1

    Input Box and Msg box Help

    Hi All,
    I am trying to write a macro that involves and inputbox and msgbox. Here is what I am trying to do:

    1. Inputbox to ask user to enter initials (Would like this to be a 2 character alpha only) with the OK/Cancel buttons option

    ----- a. If the user selects CANCEL then EXIT SUB


    2. If the user selects OK and

    ------ a. the criteria (2 character alpha) OR
    ------ b. input is blank, Then

    ---------- I. Msgbox with message “you must enter a valid 2 character initial” with the retry/cancel buttons option

    With the msgbox present:

    3. If user selects CANCEL then EXIT SUB

    4. If user selects RETRY, loop back to step 1 above until,
    ----- a. User inputs valid initials Or
    ----- b. User selects the Cancel button option

    5. If/when the user enters a valid initial, I would like to jump to a section of the code and run the remainder of the macro.


    As long as the user selects RETRY, it will continue to loop starting at STEP 1 above.

    Below is the code I have so far. I don't think that I am even close. Any help would be appreciated.

    Thanks!

    Dim Initials As String
    Dim IR As VbMsgBoxResult
    Dim MB As VbMsgBoxResult
    
    Retry:
    
    Initials = InputBox("Please Enter Your Initials for Sign Off", "Enter initials", vbOKCancel)
    If Initials = vbCancel Then GoTo ExitAll
    
         ElseIf GoTo Proceed
    
    Proceed:
         If Len(Initials) = 0 Then
              MsgBox "YOU MUST ENTER A VALID 2 CHARACTER INITIAL", vbRetryCancel, "Invalid Initials"
    
    If vbRetry = 4 Then GoTo retry
    If vbCancel = 2 Then
    
    ExitAll:
    
    Exit Sub
    
    End If
    
    End If
    
    MainCode:
    
    MY MACRO BELOW
    
    End Sub

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


    Option Explicit
    
    Sub demo()
        Dim sInitials As Variant
        Dim bGoodInput As Boolean
    
        bGoodInput = False
    
        Do While Not bGoodInput
        
            sInitials = Application.InputBox("Enter TWO initials !!!", "Demo", vbNullString, , , , , 2)
            If sInitials = False Then Exit Sub
            If Len(sInitials) <> 2 Then
                If MsgBox("You must enter a valid 2 character initial", vbCritical + vbRetryCancel, "Demo") = vbCancel Then Exit Sub
            Else
                bGoodInput = True
            End If
        Loop
    
        MsgBox "run the rest"
    
    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

Posting Permissions

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