+ Reply to Thread
Results 1 to 6 of 6

InputBox OK and Cancel Button Problem

Hybrid View

  1. #1
    Registered User
    Join Date
    07-21-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    46

    Exclamation InputBox OK and Cancel Button Problem

    I have this macro code that can protect and unprotect, It will show the input box and ask for a password. my problem is when I choose cancel, it still protect the file, the same thing with my unprotect code. here is my code below


    Protect Code

    Sub ProtectAll()
         
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
        For Each wSheet In Worksheets
            wSheet.Protect Password:=Pwd
        Next wSheet
         
    End Sub

    UnProtect Code

    Sub UnProtectAll()
         
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
        On Error Resume Next
        For Each wSheet In Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
        If Err <> 0 Then
            MsgBox "You have entered an incorect password. All worksheets could not " & _
            "be unprotected.", vbCritical, "Incorect Password"
        End If
        On Error GoTo 0
         
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: InputBox OK and Cancel Button Problem

    Try add the following:
    If len(Pwd) = 0 or Pwd = "False" then: Exit Sub
    HTH
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  3. #3
    Registered User
    Join Date
    07-21-2013
    Location
    Philippines
    MS-Off Ver
    Excel 2010
    Posts
    46

    Re: InputBox OK and Cancel Button Problem

    hi mc84excel,

    thanks for quick reply.

    where do I add the code

  4. #4
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: InputBox OK and Cancel Button Problem

    Quote Originally Posted by mackypogi View Post
    hi mc84excel,

    thanks for quick reply.

    where do I add the code
    Just below the line where you set the Pwd variable from the inputbox.

  5. #5
    Forum Contributor
    Join Date
    08-15-2012
    Location
    Australia
    MS-Off Ver
    Excel 2016
    Posts
    349

    Re: InputBox OK and Cancel Button Problem

    Try this;
    Sub ProtectAll()
         
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = InputBox("Enter your password to protect all worksheets", "Password Input")
        If Pwd = "" Then
        Exit Sub
        Else
        For Each wSheet In Worksheets
            wSheet.Protect Password:=Pwd
        Next wSheet
    End If
    End Sub
    Sub UnProtectAll()
         
        Dim wSheet          As Worksheet
        Dim Pwd             As String
         
        Pwd = InputBox("Enter your password to unprotect all worksheets", "Password Input")
        If Pwd = "" Then
        Exit Sub
        Else
        On Error Resume Next
        For Each wSheet In Worksheets
            wSheet.Unprotect Password:=Pwd
        Next wSheet
        If Err <> 0 Then
            MsgBox "You have entered an incorect password. All worksheets could not " & _
            "be unprotected.", vbCritical, "Incorect Password"
        End If
        On Error GoTo 0
    End If
    End Sub

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: InputBox OK and Cancel Button Problem

    Hi, mackypogi,

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.vbaexpress.com/forum/show...Button-Problem

    http://www.excelfox.com/forum/f2/inp...-problem-1824/

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. cancel button Application.Inputbox
    By TimlmiT in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-20-2014, 02:30 PM
  2. [SOLVED] Insert function to 'Cancel' Button in InputBox
    By leleco in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-16-2014, 11:52 AM
  3. add cancel button to inputbox
    By SarahPintal in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-03-2011, 02:27 AM
  4. Inputbox cancel button error.
    By abhay_547 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-05-2010, 06:27 AM
  5. activate cancel button in a inputbox
    By mania112 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2009, 07:09 AM

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