Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA to Protect/Unprotect Worksheet and Workbook Excel 2016

Status
Not open for further replies.

MM1963

Vendor
Feb 15, 2018
38
0
0
US
I have a workbook with many sheets. All the sheets and the workbook are protected. I'm using Excel 2016
Two of the sheets contain data from connected Access tables that needs to be refreshed periodically. However, when I click “refresh all” I get an error that the sheet is protected. After I unprotect the sheets and click “refresh all” I get an error message that the workbook is protected. I understand why because the range (and range name) where the Access table refreshes can change if the record count changes. The protected workbook prohibits this.
I need code that I can attach to a button that will:
• Use my password to unprotect worksheet1
• Use my password to unprotect worksheet2
• Use my password to unprotect the workbook
• Refresh All
• Use my password to protect worksheet1
• Use my password to protect worksheet2
• Use my password to protect the workbook
Any help would be appreciated.
 
Hi,

Turn on your macro recorder and record doing the steps that you outlined.

Assign the button to run that macro.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
There is VBA dedicated forum707.
Both workbook and worksheet objects have Protect methods with optional arguments, check object browser for details. To unprotect, use Unprotect method with password.
For worksheet you do not need to unprotct worksheets, protect again each worksheet by code with parameter UserInterfaceOnly:=True. This allows to change protected sheet by code (this feature is not stored, so you need to set it after opening the workbook).
UpdateLink method for workbook without parameter refreshes all links.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top