Scotwho
New Member
- Joined
- Jul 15, 2016
- Messages
- 4
I have a workbook that through VBA parses a worksheet into separate workbooks based upon the criteria in a specified column and then names and saves the different workbooks. No problem, this macro works great. I then have another macro that sets each workbook a password from an assigned list based upon the name of the assigned name of the workbook. The problem is that the passwords won’t advance to the next in the list, they all get assigned the first password. I know I have something just out of order or something … I am still wet behind the ears with VBA .
Here is the password macro …
Thank you for any help ...
Here is the password macro …
Code:
Sub ProtectAll()
ActiveWorkbook.Sheets("Name_Change").Activate
Dim wbk As Workbook
Dim sFileSpec As String
Dim sPathSpec As String
Dim sFoundFile As String
Dim sPW As String ' setting sPW as the Password String
Dim TotalRow As Integer
sPathSpec = "C:\Users\Test_Files\"
sFileSpec = "*.xls"
TotalRow = ActiveSheet.UsedRange.Rows.Count 'added to correct overflow error
For V = 1 To TotalRow 'set the first row to row 2
sFoundFile = Dir(sPathSpec & sFileSpec)
Do While sFoundFile <> ""
sPW = Cells(V + 1, 6).Value 'Get value of each cell in columns 6 start at row 2
Set wbk = Workbooks.Open(sPathSpec & sFoundFile)
With wbk
Application.DisplayAlerts = False
wbk.SaveAs Filename:=.FullName, Password:=sPW
Application.DisplayAlerts = True
End With
Set wbk = Nothing
Workbooks(sFoundFile).Close False
sFoundFile = Dir
Loop
Next V
ActiveWorkbook.Sheets("Front Sheet").Activate
MsgBox "You have successfully set all the passwords"
End Sub
Thank you for any help ...