none
Code to execute particular line of code in another userform in VBA. Help is required. RRS feed

  • Question

  • I have created an userform(userform1) in VBA, with combox at the top and lot of textboxes afterwards to take the inputs. I have wriiten code(uoto line 5), such that when the user selects a particular input(not FOR all INPUTS) from drop down list of combobox1 and clicked execute button, a request to login as administrator, msgbox pops up and a login screen(userform2) pops up. when correct Un & PW are entered, then code line 21 onwards in (userform1) to be executed bypassing line 6 to line 20. this was the requirement.

    i hv done all but  facing problem in executing the code in  line 21( of userform 1) when login button ( with correct UN & PW) is clicked in the  login userform(userform2)

    I hope i have explained the requirement/issue correctly.

    Tuesday, July 29, 2014 7:13 AM

All replies

  • If using Modal Userforms then no code will execute in Userform1 after Userform2.Show command is executed until after Userform2 is closed.

    To skip code you can place a label immediately before your line 21 and then use GoTo to skip the lines to that label.

    Following example code might help.

    Private Sub CommandButton1_Click()
        Dim strUserName As String
       
        strUserName = "SomeOne"   'Used for demonstration only
       
        If strUserName <> "OssieMac" Then
            UserForm2.Show
            GoTo AfterLogin     'Will skip following code to Label named AfterLogin
        End If
       

         'Other code lines here if the Login Userform is not shown.

        'Note that if Userform not shown because strUsername matched
        'then the code immediately following the End If will run
        MsgBox "After End If"
        'Note unless both userforms have Modal set to False then
        'no code after the Show command runs until Userform2 is closed
        
     'Code after the label will also run if the Login userform is not shown. The label is ignored and code continues.   
       
    AfterLogin:     'Insert label here (Note the colon (:) suffix).
        'Insert code here to run after Userform2 closed
        MsgBox "After the AfterLogin label"

    End Sub


    Regards, OssieMac

    Tuesday, July 29, 2014 7:45 AM