Finding lost PivotTables in Microsoft Excel - excel

In Microsoft Excel, when refreshing my data sources (clicking "Refresh All" in the "Connections" block under the "Data" tab), it provides me with a message saying that it can't find the data source for a PivotTable - this is understandable, as I deleted the Sheet that contained the data.
The problem is now finding the specific PivotTable to delete it, as I don't want to receive the message every time I refresh my data sources.
Any ideas?

As a simple, one-off test, I think this will work for you. It displays a message and selects the offending pivot table. Just put this code in a module in the workbook with the pivot tables:
Sub FindDatalessPivot()
Dim ws As Excel.Worksheet
Dim pt As Excel.PivotTable
For Each ws In ThisWorkbook.Worksheets
For Each pt In ws.PivotTables
With pt
On Error Resume Next
.RefreshTable
If Err.Number <> 0 Then
ws.Activate
.DataBodyRange.Select
MsgBox .Name & " in " & ws.Name & " is disconnected."
End If
On Error GoTo 0
End With
Next pt
Next ws
End Sub
Of course, you could do this by hand as well, by refreshing each pivot table individually.
I also note, that at least in Excel 2010, when I Refresh All a dialog pops up telling me which pivot table isn't connecting.

Related

Refreshing Pivot requires opening source workbook

I wrote the following sub for a workbook containing 12 pivot tables (PT) in 6 sheets, all pointing to the same external source (another workbook). It works fine, except that if I wish to manually Refresh one pivot, Excel tells me I need to open the source file. If I then open the pivot's source file, the refresh works OK.
I would prefer to have the pivot cache refreshed in the background, like it once was. What am I missing ?
Sub ChangePivotSourceData(src As String)
Dim pt As PivotTable, wks As Worksheet, pc As PivotCache
'update #1 pt in PIVOT AF
Sheet2.PivotTables(1).ChangePivotCache ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=src)
'adjust all others
For Each wks In ThisWorkbook.Worksheets
Debug.Print Now, wks.Name
For Each pt In wks.PivotTables
Debug.Print Now, pt.Name
pt.CacheIndex = Sheet2.PivotTables(1).CacheIndex
pt.RefreshTable
Next pt
Next wks
Debug.Print Now, "ChangePivotSourceData complete"
End Sub
The sub is called like this:
Workbooks.Open fn, False, True
shMenu.Range("c5").Value = fn
Application.Calculation = xlCalculationManual
'change source of ALL pivots
ChangePivotSourceData fn & "!feesIn" 'feesIn is a named range
Took me a few hours but I found a solution.
The problem is that - at first sight - you can't create a connection to an Excel range. You must have a whole sheet, or a table. In my case, the source data starts at B28 and could not be converted into a table. So I manually created the connection pointing to the whole sheet, bringing back of few rows of garbage. I then went the data/properties/definition, and
- I changed the Command type from Default to SQL
- I changed the Command text to a SQL statement like this: select * from [mySheet$b28:bt4000] where ccy is not null (note the required $ at end of sheet name)
Then I managed to get all pivots to point to that connection and finally I wrote a function to allow changing the connection source workbook name:
With ThisWorkbook.PivotCaches(1)
adoConn = Split(.Connection, ";")
adoConn(3) = "Data Source=" & fn
.Connection = Join(adoConn, ";")
.Refresh
'Debug.Print Now, .CommandText 'sql statement
End With
This way the refreshes are fast, and indeed I only need to refresh the connection to have all 12 PT updated.

How can I automatically select a data source via VBA in excel

I have an excel file with 4 sheets. Each sheet contains a pivot table that connects to an Oracle database. I need all pivot tables to update automatically at 4 am. To do this I have a macro. The problem is the macro asks me to select a data source manually. Is there a way the macro select the data source automatically?
When I execute the macro, it shows me this window 4 times:
And then:
I need the macro select the one that is marked (PRODUCCION DWH).
This is the macro:
Sub Actualiza_Reporte()
Dim pt As PivotTable
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
ThisWorkbook.Save ' Guardamos el archivo
ThisWorkbook.Close ' Para cerrar el archivo
Application.Quit ' Para cerrar Excel
End Sub
I need to update the 4 sheets automatically, without asking me to select the data source.
Thanks and regards.
Besides using macros, you could also do it with power query which is included in Excel 2016 and higher by default:
Select Data, then New Query, then Databases, then Oracle and create a connection to Oracle.
Then you only need to set the refreshing intervall like this:
Click Data again, then Connections, then Settings, then Update after X minutes.
If you want control the exact time of the oracle power query update, just configure this macro:
Sub RefreshQuery()
Dim con As WorkbookConnection
Dim Cname As String
For Each con In ActiveWorkbook.Connections
If Left(con.name, 8) = "Query - " Then
Cname = con.name
With ActiveWorkbook.Connections(Cname).OLEDBConnection
.BackgroundQuery = False 'or true, up to you
.Refresh
End With
End If
Next
End Sub
Code found here: https://stackoverflow.com/a/38653892/11971785

Date Stamp whenever someone refreshes the table

I have a pivot table in my Sheet1 connected to external sources which is also an excel sheet. All i am trying to do is to get a date and time stamp whenever someone refreshes pivot table.
I get an error Object doesn't support this property or method.
Private Sub Refresh_Click()
Dim PT As PivotTable
Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
For Each PT In WS.PivotTables
PT.RefreshTable
WS.Sheets("Month-to-Date").Range("P5") = TimeValue(Now)
Next PT
Next WS
End Sub
The problem is that WS is a Worksheet and a Worksheet does not support another Worksheet as a property.
Worksheets are properties of Workbooks so
ThisWorkbook.Sheets("Month-to-Date").Range("P5") = TimeValue(Now)
Fixes the problem
There is a fancy built-in event in Excel VBA for this. In the worksheet of the pivot table, select the event PivotTableUpdate (Event Documentation):
As far as probably there would be more than 1 pivot tables on the worksheet, it is a good idea to check which one is the refreshed one, using its name:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Select Case Target.Name
Case "PivotTable2"
Debug.Print "Write date somewhere "; Format(Now, "DD-MM-YYYY")
Case Else
Debug.Print "Not PivotTable2"
End Select
End Sub
"DateStamp" is obtained through Format(Now, "DD-MM-YYYY") and printed to the immediate window (Ctrl+G). Instead of printing there, it is a possible option to write it to a server or to a separate worksheet, getting the last cell of a row like this:
Case "PivotTable2"
Worksheets(2).Cells(lastCell, 1) = Format(Now, "DD-MM-YYYY")

PivotTable Show Details Destination

I know this may be solved with a more complicated script, but I simply want to have the .ShowDetails action for any PivotTable in my workbook (I have 15+) to send the associated data for a particular Pivot Item to a designated worksheet every time. I have this script, but I believe I have coded something incorrectly (I am receiving a procedure declaration compiling error when I attempt to execute it).
Sub Workbook_SheetBeforeDoubleClick()
Dim WS As Worksheet
If Application.Range(ActiveCell.Address).PivotCell.PivotCellType = xlPivotCellValue Then
For Each WS In ThisWorkbook.Worksheets
If WS.Name = "PivotTable Details" Then
WS.Delete
End If
Next WS
Selection.ShowDetails
ActiveSheet.Name = "PivotTable Details"
End If
End Sub
Couple things...
I believe it should be ShowDetail, not ShowDetails.
ShowDetail
is a property that needs to be set to True if you want to display
the data for the selected cell.
The following line should work.
Selection.ShowDetail = True

Renaming a new worksheet yields "Out of Memory" error - Why?

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)
You can display the memory used by a pivot cache, by using the
following User Defined Function. Store the function code in a
worksheet module. Then, on the worksheet, enter the formula:
=GetMemory(A3)/1000
replacing A3 with a cell in your pivot table. The result is displayed
in kilobytes.
Function GetMemory(rngPT As Range) As Long 'pivot table tutorial by
contextures.com Dim pt As PivotTable Set pt = rngPT.PivotTable
GetMemory = ActiveWorkbook _
.PivotCaches(pt.CacheIndex).MemoryUsed
End Function

Resources