当我完成我的excel宏时,我感到很兴奋,然后我的老板递给我一个更大的文件来尝试测试。大小差异从15,000KB到275,000KB。我只是希望它会继续运行。我错了。我得到“Excel无法使用可用资源完成此任务”错误。所以我们尝试在我的同事8核16GB RAM机器上运行它。想到这会把它撞出公园。不。同样的错误,这次在第二个数据透视表上。所以,我假设错误在我的pivottable函数中。有没有办法清理像删除缓存或其他什么的数据透视表的创建?
Function CPT(ByRef Location() As String, Name As String, Data As String, ByRef Filters() As String, ByRef Rows() As String, ByRef Columns() As String _, ByRef Values() As String, ByRef Types() As String)
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Data, Version:=xlPivotTableVersion15).createPivotTable _
TableDestination:=Worksheets(Location(0)).Range(Location(1)), TableName:=Name, _
DefaultVersion:=xlPivotTableVersion15
ActiveWorkbook.ShowPivotTableFieldList = True
'/////////////////////////////////////////////
'If it errors field name is probably incorrect
'/////////////////////////////////////////////
'Values
If (Not IsArrayEmpty(Values())) Then
For i = LBound(Values) To UBound(Values)
If (Types(i) = "Max") Then
ActiveSheet.PivotTables(Name).AddDataField ActiveSheet.PivotTables( _
Name).PivotFields(Values(i)), "Max of " & Values(i), xlMax
ElseIf (Types(i) = "Sum") Then
ActiveSheet.PivotTables(Name).AddDataField ActiveSheet.PivotTables( _
Name).PivotFields(Values(i)), "Sum of " & Values(i), xlSum
ElseIf (Types(i) = "Min") Then
ActiveSheet.PivotTables(Name).AddDataField ActiveSheet.PivotTables( _
Name).PivotFields(Values(i)), "Min of " & Values(i), xlMin
ElseIf (Types(i) = "Count") Then
ActiveSheet.PivotTables(Name).AddDataField ActiveSheet.PivotTables( _
Name).PivotFields(Values(i)), "Count of " & Values(i), xlCount
End If
Next i
End If
'Filters
If (Not IsArrayEmpty(Filters())) Then
For i = LBound(Filters) To UBound(Filters)
With ActiveSheet.PivotTables(Name).PivotFields(Filters(i))
.Orientation = xlPageField
End With
Next i
End If
'Rows
If (Not IsArrayEmpty(Rows())) Then
For i = LBound(Rows) To UBound(Rows)
With ActiveSheet.PivotTables(Name).PivotFields(Rows(i))
.Orientation = xlRowField
.Position = 1
End With
Next i
End If
'Columns
If (Not IsArrayEmpty(Columns())) Then
For i = LBound(Columns) To UBound(Columns)
With ActiveSheet.PivotTables(Name).PivotFields(Columns(i))
.Orientation = xlColumnField
End With
Next i
End If
End Function
是否有更有效的方法来获得数据透视表功能?我必须创建很多,所以我不想为每一个编写代码。