You are here:

Excel/Put Password on each workbook

Advertisement


Question
Goal:  Put a password on each workbook.
I have a workbook with several sheets.
Each sheet is saved as a separate workbook.
This VBA Code that splits worksheets into separate workbooks and save each sheet as a separate workbook.

Sub ProtectWorkbooks()
Dim strfileFullName As String
Dim mysheets  As Integer
Dim wksht As Worksheet
Dim I As Long
Dim wkshtnames()     'This is an array definition
Dim count As Integer
Dim FNAME As String
Dim myfilename As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False        
strfileFullName = ThisWorkbook.Name
mysheets = ActiveWorkbook.Sheets.count
myfilename = Left(ActiveWorkbook.Name, InStr(ActiveWorkbook.Name, ".") - 1)
Count = 0
I = 0
    For Each wksht In ActiveWorkbook.Worksheets
       I = I + 1
       ReDim Preserve wkshtnames(1 To I)
       wkshtnames(I) = wksht.Name
       wksht.Copy
       Application.ActiveWorkbook.SaveAs fileName:="C:\TEST\Excel Project\" _
       & myfilename & "_" & wksht.Name & "_" & I & "of" & mysheets & " .xlsx"
   Application.ActiveWorkbook.Close False
     Next wksht
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End sub

I want to use this password generator to put a password on each workbook:

strDP = ""
   For lRndx = 1 To Int((18 - 12 + 1) * Rnd + 18)
       If Int((2 - 1 + 1) * Rnd + 1) = 1 Then
         'symbol or number
         strDP = strDP & Chr(Int((64 - 33 + 1) * Rnd + 33))
       Else
         'lower or upper or symbol
         strDP = strDP & Chr(Int((122 - 97 + 1) * Rnd + 97))
       End If
   Next lRndx

Answer
ACHILLES,

You can make a function   

Public Function Generate()
Dim strDP As String, lRndx As Long
strDP = ""
  For lRndx = 1 To Int((18 - 12 + 1) * Rnd + 18)
      If Int((2 - 1 + 1) * Rnd + 1) = 1 Then
        'symbol or number
        strDP = strDP & Chr(Int((64 - 33 + 1) * Rnd + 33))
      Else
        'lower or upper or symbol
        strDP = strDP & Chr(Int((122 - 97 + 1) * Rnd + 97))
      End If
  Next lRndx
Generate = strDP
End Function

then call the function from your routine and use the results as the password
     Dim sPass as string

     wksht.Copy
     sPass = Generate()
      Application.ActiveWorkbook.SaveAs fileName:="C:\TEST\Excel Project\" _
      & myfilename & "_" & wksht.Name & "_" & I & "of" & mysheets & " .xlsx", _
      Password:=sPass


However, you need to incorporate some way so you know what password is being used.

Also, I have didn't attempt to locate what characters are accepted in an Excel Workbook password, so hopefully it accepts what your routine generates.

--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.