• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How to change passwords of all open workbook

nagovind

Member
Dear All,

There are many worksheets currently open

Say 5 sheets are open

All are already password protected to open it, but it is open

Password is known

How to change the old password to new one and SAVE and close it so that while opening it will be supplied with new passwords

Any codes please

Regards
Govind
 
Assuming you are talking about workbooks, and not the worksheet protection password, this code will loop through all open workbooks and change the password on them.
Code:
Sub ChangePass()
Dim newPass As String
Dim checkPass As String
Dim wb As Workbook

restart:
'Get new password
newPass = InputBox("What is the new password?")
checkPass = InputBox("Please confirm password")

'Make sure user didn't make a typo
If newPass <> checkPass Then
    MsgBox "Passwords don't match. Try again"
    GoTo restart
End If

Application.ScreenUpdating = False
For Each wb In Application.Workbooks
    'Set new password
    wb.SaveAs wb.FullName, , newPass
    'Can't close the workbook running the code
    If wb.Name <> ThisWorkbook.Name Then wb.Close
Next wb
Application.ScreenUpdating = True

End Sub
Note that it doesn't close the workbook containing this code...aka, 1 workbook will still be open at the end.
 
Dear Luke,

Thanks for the code
Yes all are open workbooks and not worksheets sorry

While running the code i got the below errors please do the needful

upload_2015-2-10_11-29-54.png
upload_2015-2-10_11-30-13.png
 
Attached is the example file
It is password protecting the PERSONAL workbook internal to excel

Please do the needful
Regards
Govind
 

Attachments

  • excel pw change.zip
    51.1 KB · Views: 1
Several questions here.

Error message about file already existing:
This is because we are doing a SaveAs over the previous file. I should have surrounded that line of code with errors turned off, like so:
Code:
   Application.DisplayAlerts = False
   wb.SaveAs wb.FullName, , newPass
   Application.DisplayAlerts = True

Can't access read-only file:
Macro assumed that all files are open and editable. If this is not the case, we can't make changes to the file...what should we do in this case? Ignore it?

Changed line to Workbook.Protect:
This does something very different. My original code (and what I thought you wanted) applied a password needed to open the file. The new line simply protects the workbook structure so someone can't delete/change sheets. Which did you really want?
 
Dear Luke,

Sorry the code i have posted is only protecting the structure, i found that after posting this reply

I need to protect the workbook with password

BELOW CODE IS PERFECTLY ALRIGHT

THANK YOU LUKE!

Code:
Sub ChangePass()

Dim newPass As String
Dim checkPass As String
Dim wb As Workbook

restart:
'Get new password
newPass = InputBox("What is the new password?")
checkPass = InputBox("Please confirm password")

'Make sure user didn't make a typo
If newPass <> checkPass Then
    MsgBox "Passwords don't match. Try again"
    GoTo restart
End If

Application.ScreenUpdating = False

For Each wb In Application.Workbooks
    'Set new password
    Application.DisplayAlerts = False
    wb.SaveAs wb.FullName, , newPass    'Can't close the workbook running the code
    Application.DisplayAlerts = False
Next wb

Application.ScreenUpdating = True
ThisWorkbook.Save
Application.Quit

End Sub
 
Last edited:
Back
Top