VBA to protect new generated workbook

BombSheels

New Member
Joined
Apr 16, 2021
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello Experts,

Just wanted to ask if it's possible to create a VBA that auto-lock the new generated workbook? Here's the scenario, I have a VBA excel (XLSM file) that generated a new workbook, but when I'm trying to protect/lock the new generated workbook, the excel that was locked is the (XLSM file). the new generated workbook is an XLS or XLSX file only.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Btw, i found a way.

NB.Protect Password:=""
NB.Worksheets("Sheet1").Cells.Locked = True
NB.Worksheets("Sheet1").Range("A1:AC300").Locked = True
NB.Worksheets("Sheet1").Protect Password:="", UserInterfaceOnly:=True
 
Upvote 0
Hello. Try this in a normal module. The idea is that you would declare the new workbook. Whether you are doing that by workbooks.add or otherwise can be modified in the Dim newWB: Set newWB = part.

VBA Code:
Sub createWB()

Dim pw As String: pw = "Password" 'Change Password to desired password wrapped in quotes

Dim sPath As String: sPath = ThisWorkbook.Path 'Change ThisWorkbook.Path to desired path"

Dim newWB As Workbook: Set newWB = Workbooks.Add 'Adds and declares a new workbook

With newWB
    'Use .Protect to protect workbook structure
        .Protect
    'Open password is the first "pw". Modify password is the second "pw"
        .SaveAs sPath & "\test.xlsx", , pw, pw
    'Close the newWB
        .Close
End With

End Sub
 
Upvote 0
Hello. Try this in a normal module. The idea is that you would declare the new workbook. Whether you are doing that by workbooks.add or otherwise can be modified in the Dim newWB: Set newWB = part.

VBA Code:
Sub createWB()

Dim pw As String: pw = "Password" 'Change Password to desired password wrapped in quotes

Dim sPath As String: sPath = ThisWorkbook.Path 'Change ThisWorkbook.Path to desired path"

Dim newWB As Workbook: Set newWB = Workbooks.Add 'Adds and declares a new workbook

With newWB
    'Use .Protect to protect workbook structure
        .Protect
    'Open password is the first "pw". Modify password is the second "pw"
        .SaveAs sPath & "\test.xlsx", , pw, pw
    'Close the newWB
        .Close
End With

End Sub
Thank you breynolds! this also works for me :)
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top