Share via

Password Protect Workbook Using VBA

Anonymous
2023-10-24T19:46:05+00:00

We have an Excel template that we use in our office for accounting purposes. It has a lot of VBA that automates our internal processes and even saves the workbook with a specific name. When my coworkers create the file, they always forget to password protect it so that only those with the password can open it. Is there a way to automate the process of password protecting the file with additional VBA code?

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2023-10-24T21:31:39+00:00

    At the end of that line, add

    , Password:="..."
    

    where ... is the password you want to set for opening the workbook.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2023-10-24T20:52:15+00:00

    Thanks.

    There is already the line: ThisWorkbook.SaveAs

    Can I add it to that? If so, what is the proper format?

    Thanks again

    Was this answer helpful?

    0 comments No comments
  3. HansV 462.6K Reputation points
    2023-10-24T20:01:25+00:00

    You write that you have code to save the workbook with a specific name. Assuming that you use a line such as

    ActiveWorkbook.SaveAs Filename:="..."

    you can specify the password in that line:

    ActiveWorkbook.SaveAs Filename:="...", Password:="..."

    Was this answer helpful?

    0 comments No comments