我有一个MS Excel宏,可以创建许多数据透视表。问题是,如果我正在过滤的值不再存在,则会生成错误并且宏停止(未创建其余的数据透视表)。违规行是:
ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle").CurrentPage = _
"SIT Cycle 2"
如果" SIT周期2"今天没有错误然后它将不在源数据中并且在宏中抛出错误。相反,我想打印" Nil"在活动表上继续。该宏最终将创建大约十个单独的数据透视表。
宏是:
'
' Cycle_2_Severity_by_Status Macro
'
'
Sheets.Add
ActiveSheet.Name = "Cycle 2 Severity by Status"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table_owssvr__1", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="'Cycle 2 Severity by Status'!R3C1", TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Cycle 2 Severity by Status").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Severity")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("SIR ID"), "Count of SIR ID", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle").CurrentPage = _
"SIT Cycle 2"
Sheets("owssvr (1)").Select
Range("A2").Select
'
' SIT_CR_Severity_by_Status Macro
'
'
Sheets.Add
ActiveSheet.Name = "SIT CR Severity by Status"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table_owssvr__1", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="'SIT CR Severity by Status'!R3C1", TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion15
Sheets("SIT CR Severity by Status").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Status")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Severity")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("SIR ID"), "Count of SIR ID", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable2").PivotFields("Test Cycle").CurrentPage = _
"SIT CR"
Sheets("owssvr (1)").Select
Range("A2").Select