+ Reply to Thread
Results 1 to 12 of 12

Add password in a button code to complete the action

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Add password in a button code to complete the action

    Hello, i want to make a button to clear specific contents from my book. I find a code and i want little help to add some security on this
    Sub ClearRanges()
    Application.ScreenUpdating = False
    
        Worksheets("Sheet1").Range("A1:G37,F9:CT9,F14:CT14").ClearContents
        Worksheets("Sheet2").Range("A3:G55,F32:CT34,F13:CT77").ClearContents
    
    Application.ScreenUpdating = True
    End Sub
    Now here, when i click on button, inform me for complete clearance of the contents ( are you sure for the contents clearance?). After the agreement, i want to ask me for personal password twice and then after clicking ok to completing the clearance.

    Ranges are indicatives.

  2. #2
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Add password in a button code to complete the action

    Hello my friend

    Try this:

    Sub ClearRanges()
    Application.ScreenUpdating = False
    
      Dim sMyPassword As String
      Dim oIBox       As Object
      Dim iPassTry    As Integer
    
      sMyPassword = "123"
      iPassTry = 0
    
      If MsgBox("Are you sure for the contents clearance?", vbYesNo + vbInformation + vbDefaultButton1) = vbYes Then
    
    TryPassword:
        If InputBox("Please enter the password to clear ranges.") <> sMyPassword Then
          iPassTry = iPassTry + 1
          If iPassTry = 2 Then
            MsgBox "Incorrect password. Call the support"
            GoTo ExitSub
          Else
            MsgBox "Incorrect password.  Please try again"
            GoTo TryPassword
          End If
        Else
          Worksheets("Sheet1").Range("A1:G37,F9:CT9,F14:CT14").ClearContents
          Worksheets("Sheet2").Range("A3:G55,F32:CT34,F13:CT77").ClearContents
          MsgBox "Success!"
        End If
      End If
      
    ExitSub:
    Application.ScreenUpdating = True
    End Sub
    If it solves your problem, please mark the Thread as [SOLVED] and add reputation.

    Give me a feedback

    Thanks :D

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Add password in a button code to complete the action

    Give this a try -

    Sub ClearRanges()
    
        Dim Ans, TypedPass1 As String, TypedPass2 As String
        Const Password As String = "XXX"
        
        Ans = MsgBox("Are You Sure You Want To Clear Ranges?", vbOKCancel)
        
        If Ans = 2 Then Exit Sub
        
        TypedPass1 = UCase(InputBox("Input Password", "Enter Password"))
        TypedPass2 = UCase(InputBox("Input Password", "Enter Password"))
        
        If TypedPass1 <> TypedPass2 Then MsgBox "Passwords Don't Match", vbCritical: Exit Sub
        If TypedPass1 <> Password Or TypedPass2 <> Password Then MsgBox "Passwords Don't Match", vbCritical: Exit Sub
        
        If Ans = 1 And TypedPass1 = TypedPass2 And TypedPass1 = Password Then
            Worksheets("Sheet1").Range("A1:G37,F9:CT9,F14:CT14").ClearContents
            Worksheets("Sheet2").Range("A3:G55,F32:CT34,F13:CT77").ClearContents
        End If
        
    End Sub
    Cheers!
    Deep Dave

  4. #4
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Re: Add password in a button code to complete the action

    Quote Originally Posted by NeedForExcel View Post
    Give this a try -

    Sub ClearRanges()
    
        Dim Ans, TypedPass1 As String, TypedPass2 As String
        Const Password As String = "XXX"
        
        Ans = MsgBox("Are You Sure You Want To Clear Ranges?", vbOKCancel)
        
        If Ans = 2 Then Exit Sub
        
        TypedPass1 = UCase(InputBox("Input Password", "Enter Password"))
        TypedPass2 = UCase(InputBox("Input Password", "Enter Password"))
        
        If TypedPass1 <> TypedPass2 Then MsgBox "Passwords Don't Match", vbCritical: Exit Sub
        If TypedPass1 <> Password Or TypedPass2 <> Password Then MsgBox "Passwords Don't Match", vbCritical: Exit Sub
        
        If Ans = 1 And TypedPass1 = TypedPass2 And TypedPass1 = Password Then
            Worksheets("Sheet1").Range("A1:G37,F9:CT9,F14:CT14").ClearContents
            Worksheets("Sheet2").Range("A3:G55,F32:CT34,F13:CT77").ClearContents
        End If
        
    End Sub
    When i change the password XXX with alphanumeric and trying to work the button it says Passwords Don't Match. What i'm doing wrong?

  5. #5
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Re: Add password in a button code to complete the action

    Both options work nicely! But, according to what I asked, closer is the code of Deep Dave. Thanks both of you for the help!
    Can i have in all msgboxs titles and not "Microsoft Excel" ?

  6. #6
    Valued Forum Contributor Gatti's Avatar
    Join Date
    06-08-2015
    Location
    Brasil, São Paulo, Ribeirão Preto
    MS-Off Ver
    365
    Posts
    346

    Re: Add password in a button code to complete the action

    @Immortal2014

    Try changing:

    Const Password As Variant = "XXX"
    So you will be abble to put any kind of value

    Don't know if it solves your problem anyway

    Don't forget to put it in " "

  7. #7
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Add password in a button code to complete the action

    When i change the password XXX with alphanumeric and trying to work the button it says Passwords Don't Match. What i'm doing wrong?
    It should work, as long as it is between ""

    I tried with this
    Const Password As String = "D12"
    and it happily worked..

    If you are still having issues, please attach the file if possible..

    See the file attached, the Password is "D12"
    Attached Files Attached Files
    Last edited by NeedForExcel; 01-20-2016 at 11:14 PM.

  8. #8
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Re: Add password in a button code to complete the action

    I'll try both solutions and if it happened again then I'll upload a sample. Thanks again.

  9. #9
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Re: Add password in a button code to complete the action

    Well, i found what was going wrong with the phrase "Passwords don't match". In my personal password using uppercase, lowercase and numbers, with no results . I try only with uppercase, numbers and special characters and the password is accepted. Why this happened?

    String & Variant works well too.

  10. #10
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Add password in a button code to complete the action

    That is coz I am using the UCase function to change any input to upper case..

    Get rid of it, and It should work as required.

  11. #11
    Forum Contributor
    Join Date
    09-18-2014
    Location
    Χαμένος στο διάστημα
    MS-Off Ver
    2016
    Posts
    383

    Re: Add password in a button code to complete the action

    In this situation can I delete the UCase from tho lines of code? Sorry for my questions but I'm not familiar with vba..

  12. #12
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Add password in a button code to complete the action

    Not exactly..

    Try if this works.

    Replace

    TypedPass1 = UCase(InputBox("Input Password", "Enter Password"))
    TypedPass2 = UCase(InputBox("Input Password", "Enter Password"))
    With

    TypedPass1 = InputBox("Input Password", "Enter Password")
    TypedPass2 = InputBox("Input Password", "Enter Password")
    Let me know if it works..

+ 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. Excel is waiting for another application to complete an OLE action.
    By Doruli in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-13-2015, 06:03 AM
  2. There isn't enough memory to complete this action.
    By matrix2280 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-18-2015, 12:51 PM
  3. There isn"t enough memory to complete this action
    By Tyso in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2015, 01:31 AM
  4. [SOLVED] Userform button action - clear,copy paste and print crashes. Please help with code
    By intex in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-05-2014, 12:16 PM
  5. Excel is waiting for another application to complete an OLE action
    By TheDude76 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-30-2013, 01:19 PM
  6. Inputbox for password protection, monte carlo simulation complete code
    By karan21 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2011, 07:44 PM
  7. If text found in list then complete action
    By CJ944 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-26-2010, 08:38 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