VBA 刷新枢轴
VBA to refresh Pivots
我正在使用代码来刷新 Pivots 并且它有效,但我被错误处理程序卡住了,它给了我:
object variable with block variable not set
这是我使用的代码:
Sub RefreshAllPivots()
On Error GoTo Errhandler
Dim pivotTable As pivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
Errhandler:
MsgBox "Error Refreshing " & pivotTable.Name
MsgBox "All Pivots Refreshed"
End Sub
非常感谢
您只想在出现错误时显示错误消息。此外,您可能希望检查分配数据透视表对象时是否发生错误:
Sub RefreshAllPivots()
On Error GoTo ErrHandler
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Next pt
ErrHandler:
If err Then
If Not pt Is Nothing Then
MsgBox "Error Refreshing " & pt.Name
Else
MsgBox "Unexpected error"
End If
Else
MsgBox "All Pivots Refreshed"
End If
End Sub
请注意,我已将您的 pivotTable
变量重命名为 pt
- 使用保留字作为变量名不是很好的做法。
抛出错误"object variable with block variable not set",因为pivotTable.Name
没有在For Each pivotTable In ActiveSheet.PivotTables
循环后声明。
此变量仅在该循环内赋值。错误处理程序之前的 Exit Sub
是 VBA:
中的最佳实践
Sub RefreshAllPivots()
On Error GoTo Errhandler
Dim pivotTable As pivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Debug.Print pivotTable.Name
Next
MsgBox "All Pivots Refreshed"
Exit Sub
Errhandler:
MsgBox "Error Refreshing " & pivotTable.Name
End Sub
我正在使用代码来刷新 Pivots 并且它有效,但我被错误处理程序卡住了,它给了我:
object variable with block variable not set
这是我使用的代码:
Sub RefreshAllPivots()
On Error GoTo Errhandler
Dim pivotTable As pivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Next
Errhandler:
MsgBox "Error Refreshing " & pivotTable.Name
MsgBox "All Pivots Refreshed"
End Sub
非常感谢
您只想在出现错误时显示错误消息。此外,您可能希望检查分配数据透视表对象时是否发生错误:
Sub RefreshAllPivots()
On Error GoTo ErrHandler
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Next pt
ErrHandler:
If err Then
If Not pt Is Nothing Then
MsgBox "Error Refreshing " & pt.Name
Else
MsgBox "Unexpected error"
End If
Else
MsgBox "All Pivots Refreshed"
End If
End Sub
请注意,我已将您的 pivotTable
变量重命名为 pt
- 使用保留字作为变量名不是很好的做法。
抛出错误"object variable with block variable not set",因为pivotTable.Name
没有在For Each pivotTable In ActiveSheet.PivotTables
循环后声明。
此变量仅在该循环内赋值。错误处理程序之前的 Exit Sub
是 VBA:
Sub RefreshAllPivots()
On Error GoTo Errhandler
Dim pivotTable As pivotTable
For Each pivotTable In ActiveSheet.PivotTables
pivotTable.RefreshTable
Debug.Print pivotTable.Name
Next
MsgBox "All Pivots Refreshed"
Exit Sub
Errhandler:
MsgBox "Error Refreshing " & pivotTable.Name
End Sub