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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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,790
Messages
6,121,608
Members
449,038
Latest member
apwr

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