SOLVED

Macro to Include Password

%3CLINGO-SUB%20id%3D%22lingo-sub-1581524%22%20slang%3D%22en-US%22%3EMacro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1581524%22%20slang%3D%22en-US%22%3E%3CP%3EI%20want%20to%20create%20a%20macro%20that%20will%20protect%20and%20unprotect%20a%20sheet%20with%20a%20password.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20to%20%22record%22%20a%20macro%20that%20had%20a%20password%20but%20the%20resulting%20macro%20had%20none.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1581524%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1581633%22%20slang%3D%22en-US%22%3ERE%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1581633%22%20slang%3D%22en-US%22%3ESheets(%22sheet1%22).Select%20ActiveSheet.Protect%20Password%3A%3D%22%22%20Sheets(%22sheet1%22).Select%20ActiveSheet.Unprotect%20Password%3A%3D%22%22%20Change%20Sheet1%20to%20the%20name%20of%20your%20sheet.%20If%20you%20want%20an%20actual%20password%2C%20place%20it%20in%20the%20%22%22.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1581698%22%20slang%3D%22en-US%22%3ERE%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1581698%22%20slang%3D%22en-US%22%3EUse%20below%20code%20Sheet1.Unprotect%20%22123%22%20Sheet1.Range(%22A1%22).Value%20%3D%20%22ABC%22%20Sheet1.Protect%20%22123%22%20where%20123%20is%20password%2C%20so%20just%20replace%20it.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1582327%22%20slang%3D%22en-US%22%3ERe%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1582327%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%20all%20for%20your%20help.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'll%20give%20your%20suggestions%20a%20shot.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F158878%22%20target%3D%22_blank%22%3E%40Frank%20Drucker%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1582629%22%20slang%3D%22en-US%22%3EBetreff%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1582629%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F722750%22%20target%3D%22_blank%22%3E%40Nikolino%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHi%3C%2FP%3E%3CP%3EI%20am%20interested%20the%20piece%20of%20vb%20you%20created%20to%20run%20the%20macro.%20I%20have%20not%20done%20much%20VB%20and%20not%20for%20a%20long%20time%20so%20I%20have%20forgotten%20most%20of%20it.%20However%2C%20I%20was%20wondering%20whether%20you%20could%20help%20whether%20I%20understand%20this%20correctly%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E1.%20you%20created%26nbsp%3B%20a%20sub%20called%20secure%20and%20attached%20it%20to%20the%20button.%26nbsp%3B%3C%2FP%3E%3CP%3E2.%20you%20created%203%20variables%20with%20different%20characteristics%20(p1%2C%20p2%2C%20i)%3C%2FP%3E%3CP%3E3.%20p1%20and%20p2%20are%20collected%20through%20an%20input%20box%3C%2FP%3E%3CP%3E4.%20then%20you%20present%20the%20user%20with%20messages%20depending%20on%20whether%20the%20p1%20or%2Fand%20p2%20are%20empty%20or%20not%20equal%20to%20one%20another%3C%2FP%3E%3CP%3E5.%26nbsp%3B%20variable%20'i'%20holds%20the%20number%20of%20sheets%20and%20then%20you%20count%20the%20sheets%20and%20populate%20'i'%20with%20that%20number%3C%2FP%3E%3CP%3E6.%20hen%20you%20tell%20excel%20to%20protect%20i%20number%20of%20sheets%20with%20password%20p1%3C%2FP%3E%3CP%3E7.%20finally%20you%20send%20message%20to%20user%20that%20sheets%20are%20protected.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESo%20that%20all%20seems%20clear%20to%20me%20so%20please%20correct%20if%20not.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20sub%20number%202%20named%20unsecure%20()%20you%20have%20the%20same%20variables%20with%20the%20same%20variable%20names%20however%20since%20declared%20inside%20the%20sub%2Fsub%20end%20does%20that%20mean%20that%20these%20are%20new%20variables%3F%20If%20you%20were%20to%20have%20wanted%20to%20use%20the%20same%20ones%20wouldn't%20you%20have%20needed%20to%20declare%20the%20variables%20outside%20the%20sub%2Fend%20sub%20statements%20to%20make%20them%20available%20to%20both%20functions%3F%3C%2FP%3E%3CP%3ESo%20the%20bit%20I%20don't%20understand%2C%20is%20that%20I%20don't%20see%20anywhere%20the%20entry%20of%20the%20second%20input%20box%20(unsecure)%20to%20be%20compared%20to%20the%20entry%20of%20the%20first%20input%20box%20(secure)%3F!%3F!%3F%20Obviously%20it%20works%20but%20I%20just%20don't%20understand%20why.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20tried%20to%20change%20the%20names%20of%20the%20variables%20in%20the%20second%20function%20(sub%20unsecure())%20and%20to%20see%20whether%20it%20still%20works%20and%20it%20does.%20Is%20the%20'Sheets(i).Unprotect%20p3'%20function%20a%20set%20function%20in%20excel%20vb%20where%20unprotect%20simply%20compares%20value%20p3%20to%20the%20value%20used%20to%20protect%20the%20worksheet%20and%20you%20don't%20need%20to%20tell%20excel%20explicitly%20to%20compare%20values%3F%26nbsp%3B%20Sorry%2C%20this%20is%20rather%20long....%3C%2FP%3E%3CP%3E5.%20if%20theses%20requirements%20are%20fulfilled%20you%20send%20message%20that%20sheet%20is%20protected%3C%2FP%3E%3CP%3E6%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1583163%22%20slang%3D%22en-US%22%3EBetreff%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1583163%22%20slang%3D%22en-US%22%3E%3CP%3ESorry%2C%20I%20didn't%20do%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWish%20I%20could%20help...%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F740158%22%20target%3D%22_blank%22%3E%40Poogermum%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1583199%22%20slang%3D%22en-US%22%3EBetreff%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1583199%22%20slang%3D%22en-US%22%3EAlthough%20I%20did%20not%20write%20it%20either%2C%20I%20think%20I%20can%20offer%20some%20additional%20explanation.%3CBR%20%2F%3E%3CBR%20%2F%3EYes%2C%20the%20variables%20declared%20in%20%22unsecure%22%20are%20separate%20from%20the%20variables%20with%20the%20same%20name%20in%20%22secure%22.%20Because%20they%20are%20declared%20inside%20%22unsecure%22%2C%20%22secure%22%20cannot%20%22see%22%20them.%20If%20the%20variable%20names%20were%20declared%20at%20the%20top%20of%20the%20module%20outside%20of%20the%20%22subs%2C%22%20then%20all%20of%20the%20procedures%20within%20the%20module%20could%20see%20them%20(and%2C%20perhaps%2C%20all%20of%20the%20procedures%20in%20all%20modules%2C%20depending%20on%20whether%20the%20variables%20are%20declared%20%22private%22%20or%20%22public%22).%3CBR%20%2F%3E%3CBR%20%2F%3EYou%20could%20save%20the%20password%20input%20in%20%22secure%22%20to%20a%20variable%20at%20the%20module%20level%20that%20both%20procedures%20can%20%22see%2C%22%20but%20that%20variable%20will%20be%20destroyed%20when%20the%20workbook%20is%20closed%2Freopened%20unless%20it%20is%20saved%20somewhere.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20you%20want%20to%20know%20more%2C%20then%20you%20could%20search%20for%20%22vba%20variable%20scope%22%20and%2C%20I%20would%20think%2C%20should%20be%20able%20to%20find%20more%20information.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20a%20bad%20password%20is%20given%20to%20%22unsecure%2C%22%20then%20vba%20will%20throw%20an%20error%20when%20it%20attempts%20to%20unprotect%20the%20sheets.%20You%20will%20note%20the%20%22on%20error%20goto%22%20statement%20right%20before%20it%20tries%20to%20unprotect%20the%20worksheets%20telling%20it%20to%20jump%20to%20the%20label%20%22fehler%3A%2C%22%20which%20tests%20the%20error%20status%20(number)%20and%20generates%20a%20message%20if%20there%20was%20an%20error%20(error%20number%20is%20not%20zero).%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1581767%22%20slang%3D%22de-DE%22%3ESubject%3A%20Macro%20to%20Include%20Password%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1581767%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F158878%22%20target%3D%22_blank%22%3E%40Frank%20printer%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22%22%3EMaybe%20this%20file%20helps%20you%2C%20I%20have%20prepared%20it%20so%20that%20you%20can%20see%20and%20play%20the%20VBA%20code.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20be%20happy%20to%20know%20if%20I%20could%20help.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3E*%20Kindly%20Mark%20and%20Vote%20this%20reply%20if%20it%20helps%20please%2C%20as%20it%20will%20be%20beneficial%20to%20more%20community%20members%20reading%20here.%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%20size%3D%222%22%3E*%20Beware%20of%20scammers%20posting%20fake%20support%20numbers%20here.%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I want to create a macro that will protect and unprotect a sheet with a password.

 

I tried to "record" a macro that had a password but the resulting macro had none.

 

 

7 Replies
Best Response confirmed by Frank Drucker (Occasional Contributor)
Solution
Sheets("sheet1").Select ActiveSheet.Protect Password:="" Sheets("sheet1").Select ActiveSheet.Unprotect Password:="" Change Sheet1 to the name of your sheet. If you want an actual password, place it in the "".
Use below code Sheet1.Unprotect "123" Sheet1.Range("A1").Value = "ABC" Sheet1.Protect "123" where 123 is password, so just replace it.

@Frank Drucker 

 

Maybe this file helps you, I have prepared it so that you can see and play the VBA code.

 

I would be happy to know if I could help.

 

 

Nikolino

I know I don't know anything (Socrates)

* Kindly Mark and Vote this reply if it helps please, as it will be beneficial to more Community members reading here.

* Beware of scammers posting fake support numbers here.

Thanks all for your help. 

 

I'll give your suggestions a shot.

 

 

@Frank Drucker 

@Nikolino 

 

Hi

I am interested the piece of vb you created to run the macro. I have not done much VB and not for a long time so I have forgotten most of it. However, I was wondering whether you could help whether I understand this correctly:

 

1. you created  a sub called secure and attached it to the button. 

2. you created 3 variables with different characteristics (p1, p2, i)

3. p1 and p2 are collected through an input box

4. then you present the user with messages depending on whether the p1 or/and p2 are empty or not equal to one another

5.  variable 'i' holds the number of sheets and then you count the sheets and populate 'i' with that number

6. hen you tell excel to protect i number of sheets with password p1

7. finally you send message to user that sheets are protected.

 

So that all seems clear to me so please correct if not. 

 

In sub number 2 named unsecure () you have the same variables with the same variable names however since declared inside the sub/sub end does that mean that these are new variables? If you were to have wanted to use the same ones wouldn't you have needed to declare the variables outside the sub/end sub statements to make them available to both functions?

So the bit I don't understand, is that I don't see anywhere the entry of the second input box (unsecure) to be compared to the entry of the first input box (secure)?!?!? Obviously it works but I just don't understand why. 

 

I tried to change the names of the variables in the second function (sub unsecure()) and to see whether it still works and it does. Is the 'Sheets(i).Unprotect p3' function a set function in excel vb where unprotect simply compares value p3 to the value used to protect the worksheet and you don't need to tell excel explicitly to compare values?  Sorry, this is rather long....

5. if theses requirements are fulfilled you send message that sheet is protected

6

Sorry, I didn't do this.

 

Wish I could help...

 

@Poogermum 

Although I did not write it either, I think I can offer some additional explanation.

Yes, the variables declared in "unsecure" are separate from the variables with the same name in "secure". Because they are declared inside "unsecure", "secure" cannot "see" them. If the variable names were declared at the top of the module outside of the "subs," then all of the procedures within the module could see them (and, perhaps, all of the procedures in all modules, depending on whether the variables are declared "private" or "public").

You could save the password input in "secure" to a variable at the module level that both procedures can "see," but that variable will be destroyed when the workbook is closed/reopened unless it is saved somewhere.

If you want to know more, then you could search for "vba variable scope" and, I would think, should be able to find more information.

If a bad password is given to "unsecure," then vba will throw an error when it attempts to unprotect the sheets. You will note the "on error goto" statement right before it tries to unprotect the worksheets telling it to jump to the label "fehler:," which tests the error status (number) and generates a message if there was an error (error number is not zero).