VBA to password protect excel file clear all its contents and returns a blank file (that is password protected)

reubenjacobc

New Member
Joined
May 26, 2023
Messages
2
Office Version
  1. 2019
Hi Everyone,

I am working to password protect all the excel files in a folder based on the file path and password entered in an excel sheet. After I run the code, the files in the folder are encrypted, but they are all blank and all the previous contents have been cleared.

Can anyone tell me what I am doing wrong?? Below is the code:
Set wslist = ThisWorkbook.Worksheets("Home_Page")

With wslist

For i = first_row To last_row - 1

'Worksheets("Processing File").Cells(i, 4) = Worksheets("Home_Page").Cells(i, 5).Value

file_name = Worksheets("Home_Page").Cells(i, 5).Value
temp_password = Worksheets("Home_Page").Cells(i, 6).Value

'ActiveWorkbook.SaveCopyAs filename:=file_name, FileFormat:=xlOpenXMLWorkbook
Set masterWB = Workbooks.Add
masterWB.SaveAs filename:=file_name, FileFormat:=xlOpenXMLWorkbook, password:=temp_password

masterWB.Close
Set WB = Nothing
Next i
End With

Thank you so much for your help!

PS: I am pretty new to VBA
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Workbooks.Add creates a new blank workbook, and that is what you are saving. It seems you just need to open existing files, add a password and save them, in which case it would be more like this (air code):

VBA Code:
Set wslist = ThisWorkbook.Worksheets("Home_Page")

With wslist

For i = first_row To last_row - 1

'Worksheets("Processing File").Cells(i, 4) = Worksheets("Home_Page").Cells(i, 5).Value

file_name = Worksheets("Home_Page").Cells(i, 5).Value
temp_password = Worksheets("Home_Page").Cells(i, 6).Value

Set masterWB = Workbooks.Open(file_name)
masterWB.Password = temp_password
masterWB.Close savechanges:=true

Next i
End With
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

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