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
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

rollis13

Active Member
Joined
Jul 30, 2012
Messages
414
Office Version
  1. 2016
Platform
  1. Windows
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
 
Solution

Fredings

New Member
Joined
Nov 30, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
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
 

Fredings

New Member
Joined
Nov 30, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
Works fantastic, thank you for your help. Best reply coming when I find the button...sorry, newbie
 

Watch MrExcel Video

Forum statistics

Threads
1,127,147
Messages
5,623,008
Members
415,946
Latest member
bellerom

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
Top