VBA code for password to open excel

%3CLINGO-SUB%20id%3D%22lingo-sub-1848372%22%20slang%3D%22en-US%22%3EVBA%20code%20for%20password%20to%20open%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1848372%22%20slang%3D%22en-US%22%3ECan%20anyone%20please%20help%20with%20a%20VBA%20code%20that%20can%20be%20used%20to%20ask%20for%20password%20when%20opening%20a%20Excel%20sheet%3F%20There%20can%20be%20a%20y%20no%20of%20sheets%20in%20the%20work%20book%20but%20none%20can%20be%20accessed%20if%20password%20is%20wrong..%3CBR%20%2F%3E%3CBR%20%2F%3EFor%20example%20when%20you%20open%20an%20Excel%20sheet%20by%20double%20clicking%20it%2C%20it%20will%20ask%20for%20enter%20the%20password%20and%20then%20only%20u%20can%20enter%20into%20this..%3CBR%20%2F%3E%3CBR%20%2F%3EI%20know%20I%20can%20do%20that%20from%20file%20menu%20other%20way%20but%20I%20need%20only%20to%20get%20it%20done%20through%20VBA.%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1848372%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%20for%20web%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1848458%22%20slang%3D%22de-DE%22%3ESubject%3A%20VBA%20code%20for%20password%20to%20open%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1848458%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F853783%22%20target%3D%22_blank%22%3E%40amitmahapatra%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CH2%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%20id%3D%22toc-hId--1212126810%22%3EVBA%20and%20macro%20scripting%3C%2FH2%3E%3CP%3EYou%20cannot%20create%20macros%20with%20VBA%20in%20Excel%20for%20the%20web%2C%20but%20you%20can%20open%20and%20edit%20VBA-enabled%20spreadsheets%20without%20removing%20(or%20corrupting)%20the%20VBA%20contained%20in%20the%20file.%20Learn%20more%20about%20%3CA%20href%3D%22https%3A%2F%2Fgo.microsoft.com%2Ffwlink%2Fp%2F%3FLinkId%3D271678%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Egetting%20started%20with%20macros%3C%2FA%3E.%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%3EYou%20could%20use%20this%20code%20with%20other%20versions.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22%22%3EOnly%20if%20it%20is%20your%20own%20file%2C%20otherwise%20you%20could%20be%20liable%20to%20prosecution.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20I%20was%20able%20to%20help%20you.%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3CP%3E*%20Kindly%20Mark%20and%20Vote%20this%20reply%20if%20it%20helps%20please%2C%20as%20it%20will%20be%20beneficial%20to%20more%20community%20members%20reading%20here%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1848459%22%20slang%3D%22de-DE%22%3ERE%3A%20VBA%20code%20for%20password%20to%20open%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1848459%22%20slang%3D%22de-DE%22%3Eups%20the%20same%20person%20as%20before...%20sry%20dont%20see%20it.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1848726%22%20slang%3D%22en-US%22%3EBetreff%3A%20VBA%20code%20for%20password%20to%20open%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1848726%22%20slang%3D%22en-US%22%3EHi..%20I%20am%20looking%20for%20adding%20a%20password.%20I%20am%20not%20looking%20to%20remove.%3CBR%20%2F%3E%3CBR%20%2F%3EIf%20we%20can't%20use%20VBA%2C%20can%20you%20please%20help%20the%20piece%20of%20code%20that%20can%20be%20used%20to%20write%20a%20macro%20that%20can%20add%20a%20password%20to%20the%20excel..%20A%20simple%20macro..%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1849083%22%20slang%3D%22de-DE%22%3ESubject%3A%20VBA%20code%20for%20password%20to%20open%20excel%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1849083%22%20slang%3D%22de-DE%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F853783%22%20target%3D%22_blank%22%3E%40amitmahapatra%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22alt-edited%22%3EHere%20is%20a%20sample%20file%20to%20insert%20the%20password.%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22alt-edited%22%3EHave%20you%20fished%20from%20the%20Internet%20before%20long%20time%20not%20had%20more%20the%20side.%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3EI%20have%20used%20this%20example%20a%20few%20times%20with%20good%20results.%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3EYou%20can%20of%20course%20make%20it%20even%20better.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22tlid-translation%20translation%22%3E%3CSPAN%20class%3D%22alt-edited%22%3EI%20hope%20to%20make%20some%20amends%20could%3C%2FSPAN%3E%20%3CLI-EMOJI%20id%3D%22lia_slightly-smiling-face%22%20title%3D%22%3Aslightly_smiling_face%3A%22%3E%3C%2FLI-EMOJI%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHope%20I%20was%20able%20to%20help%20you.%3C%2FP%3E%3CP%3ENikolino%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor
Can anyone please help with a VBA code that can be used to ask for password when opening a Excel sheet? There can be a y no of sheets in the work book but none can be accessed if password is wrong..

For example when you open an Excel sheet by double clicking it, it will ask for enter the password and then only u can enter into this..

I know I can do that from file menu other way but I need only to get it done through VBA.
11 Replies
Highlighted

@amitmahapatra 

VBA and macro scripting

You cannot create macros with VBA in Excel for the web, but you can open and edit VBA-enabled spreadsheets without removing (or corrupting) the VBA contained in the file. Learn more about getting started with macros.

 

You could use this code with other versions.
Only if it is your own file, otherwise you could be liable to prosecution.

 

Hope I was able to help you.

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

 

Highlighted
ups the same Person as before...sry dont see it.
Highlighted
Hi.. I am looking for adding a password. I am not looking to remove.

If we can't use VBA, can you please help the piece of code that can be used to write a macro that can add a password to the excel.. A simple macro..
Highlighted

@amitmahapatra 

Here is a sample file to insert the password.
Have you fished from the Internet before long time not had more the side. I have used this example a few times with good results. You can of course make it even better.

 

I hope to make some amends could

 

Hope I was able to help you.

Nikolino

I know I don't know anything (Socrates)

Highlighted

@amitmahapatra  similar to @Nikolino  I recommend showing a sheet upon opening but using that sheet to make sure the user knows that macros must be enabled and I would also put a button on that sheet to "Login".  I would also recommend that you lock the macro project so people don't circumvent your macro/password.

Highlighted

@mtarler  @Nikolino 

 

Really thanks so much for all the help and its shaping into what i am looking for but little far off.. What you are proposing is to have 1 sheet with a button or something to ask for password .. but it allows to open the excel sheet.

 

But I am looking for password to be prompt when opening the excel sheet (through Macro or VBA) as attached the attached file...

 

Do you guys think that can be possible ?

Highlighted

@amitmahapatra  So I assume you know you can do this using built in Excel protection but it sounds like you specifically want a Macro solution. In the attached I modified @Nikolino solution to hide the protected sheets.  However, I keep 1 "public" sheet visible (hide all the other sheets) so you can prompt the user with information.  It can be frustrating to a user that opens a locked workbook and sees nothing and no password prompt because of their security settings.   I would also recommend locking the macro project but left it unlocked for you to work with easier.  And if you insist you can delete/hide that public sheet also, I just don't recommend it. 

Highlighted

Hi @mtarler 

 

yes you are correct. i know how to do it but i want it through macro to use same feature while opening from OneDrive in office365 web.

 

Now the the file i opened in my desktop. i got a warning to top ribbon asking "Enable Macro" and after i click that i did not got the prompt rather the way in attached screenshot. It has frozen the screen and nothing can be done. So i had to kill it by "Task Manager".

 

Then i had uploaded to OneDrive and tried to open it from there (that was my original requirement) .. so it has nothing. it did not ask for password at all. please find the screen shot attached. 

Highlighted

@amitmahapatra So the password window was tiny for some reason.  you could actually still type in it and it would work but yeah that is a problem (4k monitor issues).  As for drive, it doesn't let macros run.  so if this is all about drive then that is a problem. In the attached I resized the password window but again I don't think this will solve you problem.

Highlighted

@amitmahapatra 

As already,  @mtarler informed you,  the combination of password with VBA and Excel with OneDrive for Web is (to put it mildly) not the most suitable.

Protect / Unprotect is a feature is something which is not supported by Onedrive. Currently, the only option is to download and then protect/unprotect and then reupload.
As far as I know, but... what do I know :).

Workbooks that are protected (encrypted with password protection) cannot be viewed in a browser window or opened via Excel Online. And to open workbooks with password protection, we need to download them to the local PC and open them with Excel.

Here is an article for your reference: Workbook features that can differ between the browser and the desktop

 

Excel worksheet protection in Excel Online

 
Attached are the instructions for doing it yourself, where no worksheet can be seen, but is only for the entire workbook.

Protect an Excel file - For do-it-yourself

Excel for Microsoft 365 Excel for Microsoft 365 for Mac Excel 2019 Excel 2016 Excel 2019 for Mac Excel 2013 Excel 2010 Excel 2007 Excel 2016 for Mac

To prevent others from accessing data in your Excel files, protect your Excel file with a password. 

https://support.microsoft.com/en-us/office/protect-an-excel-file-7359d4ae-7213-4ac2-b058-f75e9311b59...

 

Protect a worksheet

That was all I could give you in terms of information.
If you want to use VBA but with changes that you want, please simply give us a message.
 

Thank you for your understanding and patience

 

Wish you a nice day.

 

Nikolino

I know I don't know anything (Socrates)

Highlighted

@Nikolino and  @mtarler .. thanks so much for all your help and time.