VBA password protect Excel workbook

VBA to password protect a workbook. Can be used to automatically protect multiple workbooks by looping through all the workbooks and password protecting them.

Update xlFileSaveName = “Z:\test.xls”   with the filepath to your workbook
Update CurrMgrPwrd = “TestPW” with your password

 

Dim xlFileSaveName As String
Dim wkb As Excel.Workbook
xlFileSaveName = “Z:\test.xls"
Set wkb = xlApp.Workbooks.Open(xlFileSaveName)
CurrMgrPwrd = “TestPW”
wkb.Password = CurrMgrPwrd
wkb.Save
wkb.Close