I’m trying to make a new sheet and give it a name using the following code:
Sub CREATEWORKSHEETS()
For Each PC In ActiveWorkbook.PivotCaches
On Error Resume Next
PC.Refresh
Next PC
PvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, "Pivot Data!$AF:$AO")
Sheets("P&L Pivot").Select
Application.DisplayAlerts = False
On Error Resume Next
Sheets("MAIN").Delete
Application.DisplayAlerts = True
On Error GoTo 0
Sheets.Add.Name = "MAIN"
End Sub
But I’m always getting an “Out of Memory” error whenever it tries to create the new “MAIN” sheet. Splitting this into the following:
Sheets.Add
ActiveSheet.Name = "MAIN"
Suggests that the renaming operation is the culprit, but I can’t figure out why. Does anybody have any idea why this might be happening?
setting a pivotcache equal to the entire columns of AF to AO is causing you to hit the excel memory limit. Try setting the pivot cache to a dynamically named range to you dont have a bunch of extra cells being held in cache
If you’re in doubt of how much memory your pivotcache may be using try this: (copied from this website)