+ Reply to Thread
Results 1 to 9 of 9

error with MsgBox

Hybrid View

  1. #1
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    60

    error with MsgBox

    Hi Friends,

    I worte a procedure to protect the worksheet. When we clik on command button it asks the password but if I click on cancel button procedure is exiting.

    Please help me with this code.

    Private Sub CommandButton1_Click()
    Dim pword As String
    Dim msg As String
    Dim ans As Integer
    
    TryAgain:
        ' Set up error handling if the entered password is wrong then control goes to the BadEntry label.
        On Error GoTo BadEntry
        'Assigned inputbox function to pword variable
        pword = InputBox("Enter the password", "Unlock the source sheet")
           
        'checks for password to unprotect the sheet
        If pword = "sadc" Then
            Sheets("source").Unprotect pword
        Exit Sub
        End If
    
    BadEntry:
        'Assigned msgbox information to msg variable
        msg = "Wrong Password, Try again?"
        'Assigned msgbox information to ans variable
        ans = MsgBox(msg, vbYesNo, "Information")
        If ans = vbNo Then
           Exit Sub
        End If
        'It checks weather user selected yes or no command butoons if the user selected "Yes"
         'then the control goes to AryAgrain label.
        If ans = vbYes Then GoTo TryAgain
            
         'Then Resume TryAgain
    'Exit Sub
    End Sub
    Thans and Regards
    Ramesh
    Last edited by ramserp; 12-10-2009 at 07:03 AM. Reason: to make title SOLVED

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error with MsgBox

    I think this is what you want
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim pword  As String
        Dim msg    As String
        Dim ans    As Integer
    
    TryAgain:
        ' Set up error handling if the entered password is wrong then control goes to the BadEntry label.
        On Error GoTo BadEntry
        'Assigned inputbox function to pword variable
        pword = InputBox("Enter the password", "Unlock the source sheet")
    
        'checks for password to unprotect the sheet
        Select Case pword
            Case "sadc"
                Sheets("source").Unprotect pword
                Exit Sub
            Case "": GoTo TryAgain
           ' Case Else: GoTo BadEntry
        End Select
    
    BadEntry:
        'Assigned msgbox information to msg variable
        msg = "Wrong Password, Try again?"
        'Assigned msgbox information to ans variable
        ans = MsgBox(msg, vbYesNo, "Information")
        If ans = vbNo Then
            Exit Sub
        End If
        'It checks weather user selected yes or no command butoons if the user selected "Yes"
        'then the control goes to AryAgrain label.
        If ans = vbYes Then GoTo TryAgain
    
        'Then Resume TryAgain
        'Exit Sub
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: error with MsgBox

    Hi Roy,

    When I click on Cancel button it is not closing the inputbox window.


    I have attached my excel please verify it.


    Thanks & Regards
    Ramesh
    Attached Files Attached Files

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error with MsgBox

    The password for the attched sheet is not sadc

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error with MsgBox

    I've changed the password to sadc & the code works ine
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim pword  As String
        Dim msg    As String
        Dim ans    As Integer
    
    TryAgain:
        ' Set up error handling if the entered password is wrong then control goes to the BadEntry label.
        On Error GoTo BadEntry
        'Assigned inputbox function to pword variable
        pword = InputBox("Enter the password", "Unlock the source sheet")
    
        'checks for password to unprotect the sheet
        Select Case pword
            Case "sadc"
                Sheets("source").Unprotect pword
                Exit Sub
            Case "": GoTo TryAgain
            Case Else: GoTo BadEntry
        End Select
    
    BadEntry:
        'Assigned msgbox information to msg variable
        msg = "Wrong Password, Try again?"
        'Assigned msgbox information to ans variable
        ans = MsgBox(msg, vbYesNo, "Information")
        If ans = vbNo Then
            Exit Sub
        End If
        'It checks weather user selected yes or no command butoons if the user selected "Yes"
        'then the control goes to AryAgrain label.
        If ans = vbYes Then GoTo TryAgain
    
        'Then Resume TryAgain
        'Exit Sub
    End Sub

  6. #6
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: error with MsgBox

    Hi Roy,

    When I click on Cancel button the inputbox window is not closing. Please kindly correct the following code.
    If ans = vbNo Then
            Exit Sub
        End If
    Thanks & Regards
    Ramesh

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error with MsgBox

    Your code was written that way, all I've done is make it work as the code seemed to want it to. Have you edited someone else's code?

    The idea with the Forum is to provide help, you should study the code & try to make such simple changes yourself
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim pword  As String
        Dim msg    As String
        Dim ans    As Integer
    
    TryAgain:
        ' Set up error handling if the entered password is wrong then control goes to the BadEntry label.
        On Error GoTo BadEntry
        'Assigned inputbox function to pword variable
        pword = InputBox("Enter the password", "Unlock the source sheet")
    
        'checks for password to unprotect the sheet
        Select Case pword
            Case "sadc"
                Sheets("source").Unprotect pword
                Exit Sub
            Case "": Exit Sub
            Case Else: GoTo BadEntry
        End Select
    
    BadEntry:
        'Assigned msgbox information to msg variable
        msg = "Wrong Password, Try again?"
        'Assigned msgbox information to ans variable
        ans = MsgBox(msg, vbYesNo, "Information")
        If ans = vbNo Then
            Exit Sub
        End If
        'It checks weather user selected yes or no command butoons if the user selected "Yes"
        'then the control goes to AryAgrain label.
        If ans = vbYes Then GoTo TryAgain
    
        'Then Resume TryAgain
        'Exit Sub
    End Sub
    Last edited by royUK; 12-10-2009 at 05:54 AM.

  8. #8
    Registered User
    Join Date
    03-21-2009
    Location
    Bangalore
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: error with MsgBox

    Hi Roy,

    Thank you very much for spending your valuable time to reply my posts.

    I wrote this procedure myself by refering a similar procedure. I am newbie of VBA so I am facing some problems with logic.


    Thanks & Regards
    Ramesh

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: error with MsgBox

    No problem. try to understand the code 7 post back with specific questions on anyhing that you do not understandIf you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

    You might also like to rate any answer(s)

+ 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