My team is creating a "Customer Dashboard" in Excel that displays metrics in the form of PivotTables on various tabs. We have created a couple of SQL Server connections to pull data that we need to populate the PivotTables. These connections use stored procs and the parameters for the stored procs are gathered from a few cells. The stored proc looks like this:
{CALL OPE.OPE.uspCSDashboard(?,?,?,?)}
The report has been running extremely well and our internal customer loves it. We just ran into an issue recently when they tried to pull a large system. The large system is about 56 columns by ~65,000 rows. The result is that Excel appears to crash or timeout. When I hit "run report" it acts like its working normally, then it greys out and says (not responding) at the top. Sometimes it will recover when pulling smaller datasets (56X28,000) but it doesn't seem to recover after waiting approximately five minutes.
When running the stored proc in SQL Server for the large system. It completed in about seven seconds.
Does anyone know why it takes so long? And what can I do to fix the crashing error when running for a large system? Below is all of the code:
Sub FilterPivotField(Field As PivotField, Value)
Application.ScreenUpdating = False
With Field
On Error Resume Next
If .Orientation = xlPageField Then
.CurrentPage = Value
ElseIf .Orientation = xlRowField Or .Orientation = xlColumnField Then
Dim i As Long
On Error Resume Next ' Needed to avoid getting errors when manipulating PivotItems that were deleted from the data source.
' Set first item to Visible to avoid getting no visible items while working
.PivotItems(1).Visible = True
For i = 2 To Field.PivotItems.Count
If .PivotItems(i).Name = Value Then _
.PivotItems(i).Visible = True Else _
.PivotItems(i).Visible = False
Next i
If .PivotItems(1).Name = Value Then _
.PivotItems(1).Visible = True Else _
.PivotItems(1).Visible = False
End If
End With
Application.ScreenUpdating = True
End Sub
Sub RunReport()
'Aliasing PivotTable Function
Dim pt As PivotTable
'Turn Screen Updates Off
Application.ScreenUpdating = False
'Unprotect Sheets
Worksheets("Hospital Dashboard").Unprotect ("escan")
Worksheets("Reports Summary").Unprotect ("escan")
Worksheets("Exclusion Report").Unprotect ("escan")
Worksheets("Billing Deadline Report").Unprotect ("escan")
'Unhide Certain Tabs
Sheets("DetailData").Visible = True
Sheets("HiddenPivotTables").Visible = True
'Refresh Tables
Application.Goto reference:="Table_Query_from_CustomerDashboard"
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Application.Goto reference:="Table_Query_from_CustomerDashboard_1"
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
'Refresh PT Tables
For Each Worksheet In ThisWorkbook.Worksheets
For Each pt In Worksheet.PivotTables
pt.PivotCache.Refresh
Next pt
Next
'call filter
FilterPivotField Worksheets("Hospital Dashboard").PivotTables("PivotTable7").PivotFields("IsCoded"), "0"
FilterPivotField Worksheets("Hospital Dashboard").PivotTables("PivotTable7").PivotFields("IsInvoiced"), "0"
FilterPivotField Worksheets("Hospital Dashboard").PivotTables("PivotTable8").PivotFields("IsCoded"), "0"
FilterPivotField Worksheets("Hospital Dashboard").PivotTables("PivotTable8").PivotFields("IsInvoiced"), "0"
FilterPivotField Worksheets("Billing Deadline Report").PivotTables("PivotTable1").PivotFields("IsCoded"), "0"
FilterPivotField Worksheets("Billing Deadline Report").PivotTables("PivotTable1").PivotFields("IsExcluded"), "0"
FilterPivotField Worksheets("HiddenPivotTables").PivotTables("PivotTable5").PivotFields("IsCoded"), "0"
FilterPivotField Worksheets("HiddenPivotTables").PivotTables("PivotTable5").PivotFields("IsExcluded"), "0"
'Hide Certain Tabs
Sheets("DetailData").Visible = False
Sheets("HiddenPivotTables").Visible = False
'Protect Sheets
Worksheets("Hospital Dashboard").Protect "escan", _
DrawingObjects:=False, _
Contents:=True, _
Scenarios:=False, _
userInterfaceOnly:=False, _
AllowFormattingCells:=False, _
AllowFormattingColumns:=False, _
AllowFormattingRows:=False, _
AllowInsertingColumns:=False, _
AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, _
AllowDeletingColumns:=False, _
AllowDeletingRows:=False, _
AllowSorting:=False, _
AllowFiltering:=False, _
AllowUsingPivotTables:=True
Worksheets("Reports Summary").Protect "escan", _
DrawingObjects:=False, _
Contents:=True, _
Scenarios:=False, _
userInterfaceOnly:=False, _
AllowFormattingCells:=False, _
AllowFormattingColumns:=False, _
AllowFormattingRows:=False, _
AllowInsertingColumns:=False, _
AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, _
AllowDeletingColumns:=False, _
AllowDeletingRows:=False, _
AllowSorting:=False, _
AllowFiltering:=False, _
AllowUsingPivotTables:=True
Worksheets("Exclusion Report").Protect "escan", _
DrawingObjects:=False, _
Contents:=True, _
Scenarios:=False, _
userInterfaceOnly:=False, _
AllowFormattingCells:=False, _
AllowFormattingColumns:=False, _
AllowFormattingRows:=False, _
AllowInsertingColumns:=False, _
AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, _
AllowDeletingColumns:=False, _
AllowDeletingRows:=False, _
AllowSorting:=False, _
AllowFiltering:=False, _
AllowUsingPivotTables:=True
Worksheets("Billing Deadline Report").Protect "escan", _
DrawingObjects:=False, _
Contents:=True, _
Scenarios:=False, _
userInterfaceOnly:=False, _
AllowFormattingCells:=False, _
AllowFormattingColumns:=False, _
AllowFormattingRows:=False, _
AllowInsertingColumns:=False, _
AllowInsertingRows:=False, _
AllowInsertingHyperlinks:=False, _
AllowDeletingColumns:=False, _
AllowDeletingRows:=False, _
AllowSorting:=False, _
AllowFiltering:=False, _
AllowUsingPivotTables:=True
'Unhide Detail Data
Worksheets("DetailData").Activate
Rows("2:500000").Hidden = False
'Getting back to home sheet
Worksheets("Home").Select
'Setting data last update to value
Worksheets("home").Range("c6").Value = "=OFFSET(DetailData!aq8,0,0)"
'Message Box to let the CSR know data has been refreshed
Dim Done As String
Done = "Data is finished updating!"
MsgBox (Done)
End Sub