I have a sample spreadsheet where I have 3 worksheets that I would like to save as stand alone workbooks, but I would also like to password protect each workbook with a unique password.
I have a worksheet within the master called 'Setup' and I have the worksheet names stored in B2:B4 and the passwords are stored in C2:C4.
The below sample code will create the files as desired, but only the first password in the list is applied to all workbooks that are created.
Any help would be much appreciated, thanks in advance:
Option Explicit
Sub SaveSheets()
Dim worksheet_list As Variant
Dim worksheet_name As Variant
Dim pwd_list As Variant
'Dim pwd_name As Variant
Dim new_workbook As Workbook
Dim saved_folder As String
saved_folder = "C:\Users\warleque\OneDrive - Confluent Health\Desktop\TestExcel\"
pwd_list = ThisWorkbook.Sheets("Setup").Range("C2:C4")
worksheet_list = ThisWorkbook.Sheets("Setup").Range("B2:B4")
For Each worksheet_name In worksheet_list
On Error Resume Next
Set new_workbook = Workbooks.Add
ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
new_workbook.SaveAs saved_folder & worksheet_name & ".xlsx", 51, password:=pwd_list
new_workbook.Close False
Next worksheet_name
MsgBox "Export complete", vbInformation
End Sub
I have a worksheet within the master called 'Setup' and I have the worksheet names stored in B2:B4 and the passwords are stored in C2:C4.
The below sample code will create the files as desired, but only the first password in the list is applied to all workbooks that are created.
Any help would be much appreciated, thanks in advance:
Option Explicit
Sub SaveSheets()
Dim worksheet_list As Variant
Dim worksheet_name As Variant
Dim pwd_list As Variant
'Dim pwd_name As Variant
Dim new_workbook As Workbook
Dim saved_folder As String
saved_folder = "C:\Users\warleque\OneDrive - Confluent Health\Desktop\TestExcel\"
pwd_list = ThisWorkbook.Sheets("Setup").Range("C2:C4")
worksheet_list = ThisWorkbook.Sheets("Setup").Range("B2:B4")
For Each worksheet_name In worksheet_list
On Error Resume Next
Set new_workbook = Workbooks.Add
ThisWorkbook.Worksheets(worksheet_name).Copy new_workbook.Worksheets(1)
new_workbook.SaveAs saved_folder & worksheet_name & ".xlsx", 51, password:=pwd_list
new_workbook.Close False
Next worksheet_name
MsgBox "Export complete", vbInformation
End Sub