Macro, how to protect sheet, but allow pivot?

G

Guest

I have an event macro Workbook_Open() that contains:

For Each wSheet In Worksheets
wSheet.Protect Password:="xxxxxx", _
UserInterFaceOnly:=True, _
AllowUsingPivotTables:=True, _
AllowSorting:=True
Next wSheet

This is to protect the worksheets. I have a sheet with a summary pivot table
and have assigned a macro to a button to refresh the pivot with

ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh

This fails due to the protection, I assumed the UserInterFaceOnly:=True
would allow the macro to run OK. I get run-time error 1004 and a message
"That command cannot be performed while a protected sheet contains another
PivotTable report based on the same source data." "To remove protection from
the sheet that has the other report....."

But there is only one pivot table in the workbook.

Any ideas how to run the pivot refrsh macro?

Many thanks
 
M

Mark H. Shin

Try unprotecting your worksheet (using VBA) just before your refresh code.
Then protecting the sheet again after your refresh code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top