1

I am trying to save the sheet with password but I am not able to unlock it with same password, can someone please help me out with getting this resolved.

Sub ClearData()   
ActiveSheet.Unprotect Password = "Kiran123"
Sheets("Filter").Select
Range("A11").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Clear
Sheets("Filter").Select
Range("C3").Select
Selection.Clear

ActiveSheet.Protect Password = "Kiran123"
End Sub

Thanks for checking this, However when I have tried with above code(my code) it doesn't work. However when you try it with only below sub it works, why such ambiguity in both the codes. any specific reason why this is occurring ? would be great if I can get an code to lock the sheet with password.

Sub test()
ActiveSheet.Protect Password:="Kiran123"
ActiveSheet.Unprotect Password:="Kiran123"
End Sub
1
  • Are you using On Error Resume Next anywhere in your code? As @Eirikdaude's answer mentions your code shouldn't compile, much less run and this is the only reason I can think of that would cause this. Commented Feb 17, 2015 at 12:42

3 Answers 3

1

You are switching focus away from your ActiveSheet by using

Sheets("Filter").Select

Instead you can keep a reference to the original spreadsheet then use the reference for your passworded sheet deprotection and protection...

Sub ClearData()

    Dim ws As Worksheet
    Dim pw As String

    Set ws = ActiveSheet
    pw = "Kiran123"

    ws.Unprotect (pw)

    Sheets("Filter").Select
    Range("A11").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Clear
    Range("C3").Select
    Selection.Clear

    ws.Protect (pw)
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

This sub worked for me:

Sub test()
  ActiveSheet.Protect Password:="Kiran123"
  ActiveSheet.Unprotect Password:="Kiran123"
End Sub

The reason this works, and your sub doesn't, is that in mine I am assigning a named argument while I'd think that the lines in your sub tries to set what precedes the equal sign equal to what comes after, that is the difference between := and =. Honestly, I am a bit surprised your sub ran at all, it certainly didn't run for me.

2 Comments

Edit that information into your answer rather than leaving it as a comment, you can do that by clicking the small 'edit' hyperlink beneath the body of your answer. If possible with an explanation of why that colon makes all the difference.
@Aiken Done, not entirely sure if my explanation is 100% correct, but it's the best I could do. Feel free to come with further input if there's something I can do to improve the answer.
0

The ActiveSheet might not be the "Filter" tab...

Worksheets("Filter").Unprotect Password = "Kiran123"

or switch the second and the third lines in you code posted above :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.