0

我有这个代码:

Private Sub Workbook_Open()

Dim wSheet As Worksheet

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

End Sub

它保护工作簿中的所有工作表,但允许宏运行。
但是,这段代码:

wb.RefreshAll 'I've declared and set wb accordingly.

即使UserIntergFaceOnly设置为. 也不执行True

4

2 回答 2

1

userinterfaceonly旗帜肯定不会像我期望的那样工作。一般来说,我设置它,但我经常发现我必须用unprotectand括起来protect。每当我遇到错误或无法做某事时,我都会尝试第一件事。

对于缺乏细节,我深表歉意,我会在调查我的代码后回复。老实说,由于缺乏 VBA 文档,我通常忙于试验和错误,以至于我还没有时间记录这种类型的行为。但我会在不久的将来。

但与此同时,我也尽量确保对象引用是合格的,因此,例如,在你的情况下,我会尝试:

Thisworkbook.Refreshall

或者

Application.Thisworkbook.Refreshall

或者甚至可能

application.workbooks(application.thisworkbook.name)

做不到这一点,括号unprotect/protect contents:=True, userinterfaceonly:=True

于 2013-11-06T07:05:34.487 回答
1

尝试这个

Private Sub Workbook_Open()
    Dim wSheet As Worksheet
    Dim pt As PivotTable

    For Each wSheet In Worksheets
        wSheet.Protect Password:="Wordpass", _
        UserInterFaceOnly:=True
    Next wSheet
End Sub

'~~> Change "Sheet1" to the relevant sheet which has the pivot   
Sub UpdatePivot()
    With ThisWorkbook.Sheets("Sheet1")
        For Each pt In .PivotTables
            pt.RefreshTable
            DoEvents
        Next pt
    End With
End Sub
于 2013-11-06T06:57:08.540 回答