问题
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?
回答1:
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 :
To not protect one sheet - have it at position 1 and start the loop at 2...
来源:https://stackoverflow.com/questions/51620146/export-access-queries-to-password-protected-excel-workbook