
Question:
I'm attempting to export 2 queries from an Access database to 2 tabs in an Excel workbook. I want that workbook to be password-protected. I'm having difficulty making it password protected in VBA. Here's my VBA so far:
Public Function ExportToExcel()
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
TableName:="q1_Get_Load_Data", _
FileName:="C:\Users\...\POPs_Reports.xlsx")
Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
TableName:="q2_Number_by_Alpha", _
FileName:="C:\Users\...\POPs_Reports.xlsx")
End Function
Is there a way to add code to make the workbook password protected?
Answer1:This is the code I use to protect 40 sheets in 6 different workbooks:
Sub protectAll()
Dim myCount
Dim i
myCount = Application.Sheets.Count
Sheets(1).Select
For i = 1 To myCount
ActiveSheet.Protect "the_password", True, True
If i = myCount Then
End
End If
ActiveSheet.Next.Select
Next i
End Sub Sub UnprotectAll() Dim myCount Dim i myCount = Application.Sheets.Count Sheets(1).Select For i = 1 To myCount ActiveSheet.Unprotect "the_password" If i = myCount Then End End If ActiveSheet.Next.Select Next i End Sub
So, for the cells you want access to i.e. not protected then select the cell (or cells) and go to format cells and un-tick the "locked" checkbox, (test it on a spare sheet!) see :
<a href="https://i.stack.imgur.com/5jrgB.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/5jrgB.png" data-original="https://i.stack.imgur.com/5jrgB.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
To not protect one sheet - have it at position 1 and start the loop at 2...