excel - 刷新受保护工作表上的数据透视 - 即使在 VBA 中取消保护也会出错
问题描述
我有一个数据透视表,它会在工作表激活时自动刷新,其中包含来自另一个受保护选项卡的数据。即使我在刷新之前取消保护选项卡,我仍然得到:
Run-time error '1004':
Cannot edit PivotTable on protected sheet.
我的 VBA:
Private Sub Worksheet_Activate()
Sheets("Extract").Unprotect "password"
ActiveSheet.PivotTables("pvt_Activity").PivotCache.Refresh
Sheets("Extract").Protect Password:="password", Contents:=True, Scenarios:=True
End Sub
把我的头撞在墙上 - 为什么这不起作用?
干杯
解决方案
由于数据透视表位于受保护的工作表中,因此您会收到此错误,因此您需要在刷新之前取消保护它们
所以在每张纸上都有一个刷新按钮并在下面分配这个宏,
g_sPassword->是取消保护工作表的密码,我已经声明为变量,所以我直接使用它
子 RefreshPtInFirstsheet()
Call RefreshPivotTables(sheetname1)
结束子
子 RefreshPtInSecondsheet()
Call RefreshPivotTables(sheetname2)
结束子
子刷新数据透视表(sht 作为工作表)
Dim pt As pivotTable
sheetname1.Unprotect g_sPassword
sheetname2.Unprotect g_sPassword
For Each pt In sht.PivotTables
pt.PivotCache.Refresh
Exit For
Next pt
sheetname1.Protect g_sPassword
sheetname2.Protect g_sPassword
MsgBox "Pivot Table Updated"
结束子
推荐阅读
- android - 如何在Android中命名协程?
- python - 将数据从网络抓取到 Excel 的问题
- sql - SQL查询过滤多次购买相同产品且数量大于5的客户
- xamarin - 如何在 Xamarin 中将绑定值从一个类传递到另一个类?
- python-3.x - 从字符串中删除字符(DataFrame)
- vba - 确定 powerpoint 中的单位设置
- javascript - 如何解决Node js unicode转换
- visual-studio-code - 如何在 vscode 中选择非第一建议使用键盘?
- reactjs - 自定义 react-admin 拖放列表
- javascript - Recharts — 有没有办法在 RadarChart 的图表和标签之间添加填充?