VBA to save workbook sheets with separate files and passwords

Fredings

New Member
Joined
Nov 30, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
I have the following code which saves each worksheet in a workbook as separate files with a password. However each file has the same password and I need them to have a different password. How can I achieve this? A separate sheet to store the passwords on would be OK but I then need to know how to reference the words in the loop. Better still would be to have the passwords in the VBA. Thanks.

Sub saveworksheets()

Dim ws As Worksheet
Dim wb As Workbook

For Each ws In ThisWorkbook.Worksheets

Set wb = Workbooks.Add
wb.SaveAs ThisWorkbook.Path & "\" & ws.Name & ".xlsx", Password:="12345"

ws.Copy before:=wb.Worksheets(1)
wb.Close savechanges:=True

Next ws



'
' Keyboard Shortcut: Ctrl+x
'
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
This is what your updated macro could look like, have a try.
Now all you have to do is decide how many passwords you need if the number of sheets is static or change the method if it needs to be dynamic.
VBA Code:
Option Explicit

Sub SaveWorksheets()

    Dim ws     As Worksheet
    Dim wb     As Workbook
    Dim pwd()  As Variant
    Dim x      As Integer

    pwd() = Array("12345", "23456", "34567", "45678", "56789") '<= this is for max 5 sheets, change as needed (no limit)
    For Each ws In ThisWorkbook.Worksheets
        Set wb = Workbooks.Add
        wb.SaveAs ThisWorkbook.Path & "\" & ws.Name & ".xlsx", Password:=pwd(x)
        ws.Copy before:=wb.Worksheets(1)
        wb.Close savechanges:=True
        x = x + 1
    Next ws
    '
    ' Keyboard Shortcut: Ctrl+x
    '
End Sub
 
Upvote 0
Solution
This is what your updated macro could look like, have a try.
Now all you have to do is decide how many passwords you need if the number of sheets is static or change the method if it needs to be dynamic.
VBA Code:
Option Explicit

Sub SaveWorksheets()

    Dim ws     As Worksheet
    Dim wb     As Workbook
    Dim pwd()  As Variant
    Dim x      As Integer

    pwd() = Array("12345", "23456", "34567", "45678", "56789") '<= this is for max 5 sheets, change as needed (no limit)
    For Each ws In ThisWorkbook.Worksheets
        Set wb = Workbooks.Add
        wb.SaveAs ThisWorkbook.Path & "\" & ws.Name & ".xlsx", Password:=pwd(x)
        ws.Copy before:=wb.Worksheets(1)
        wb.Close savechanges:=True
        x = x + 1
    Next ws
    '
    ' Keyboard Shortcut: Ctrl+x
    '
End Sub
 
Upvote 0
Works fantastic, thank you for your help. Best reply coming when I find the button...sorry, newbie
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,988
Members
448,538
Latest member
alex78

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