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")
Related
I have a spreadsheet and I created a sub function (please see below) to make a call to update specific pivot tables. One particular pivot table ("DataModel") has text as values, so, when I created that pivot table I added it to the data model then did a "Add Measure" and created a line code =CONCATENATEX(DataModel,[DataValue],”, “) so I can pivot on the text value.
Anyway, when I run the code below, it updates all the pivot tables I want except for the one made from the data model. Does anyone know how I can use Excel VBA to automate the pivot table made from the data model?
---- Call to module ---
Call RefreshPivots("DataModel")
---- Excel VBA Code ---
Public Sub RefreshPivots(ByVal sheetName As String)
Dim sheetExists As Boolean
sheetExists = False
For Each ws In Worksheets
If ws.Name = sheetName Then sheetExists = True
Next ws
If sheetExists Then
Sheets(sheetName).Visible = True
Sheets(sheetName).Activate
For Each pivotTab In ActiveSheet.PivotTables
pivotTab.PivotCache.Refresh
Next pivotTab
End If
End Sub
Use the RefreshTable method of the PivotTable object instead.
By the way, the pivot table worksheet does not need to be visible in order to refresh the pivot table.
Also, it's considered good programming practice to follow the 'Dedication of Duty' principle. It basically says that a function should be dedicated to do one thing, and nothing else.
So, for example, RefreshPivots should only refresh the pivot tables. Checking whether the worksheet exists should be done before calling your function to update the pivot tables.
I have re-written your code accordingly...
Option Explicit
Sub test()
Dim worksheetName As String
worksheetName = "DataModel"
If Not WorksheetExists(worksheetName) Then
MsgBox "'" & worksheetName & "' not found!", vbExclamation
Exit Sub
End If
RefreshWorksheetPivots worksheetName
'make worksheet visible, if so desired
End Sub
Public Function WorksheetExists(ByVal worksheetName As String) As Boolean
Dim ws As Worksheet
For Each ws In Worksheets
If UCase(ws.Name) = UCase(worksheetName) Then 'case-insensitive comparison
WorksheetExists = True
Exit Function
End If
Next ws
WorksheetExists = False
End Function
Public Sub RefreshWorksheetPivots(ByVal worksheetName As String)
Dim pt As PivotTable
For Each pt In Worksheets(worksheetName).PivotTables
pt.RefreshTable
Next pt
End Sub
I use this code to create a new sheet with a dynamic name based on the real-time but it is not successful run, show
error 1004 "application-defined or object-defined error".
Private Sub Compute_Click()
With ThisWorkbook
Worksheets("Data").Range("A1").Value = Now
If IsDate(Worksheets("Data").Range("A1")) Then
Set Worksheet = .Sheets.Add(After:=.Sheets(.Sheets.Count))
Worksheet.Name = Format(Range("A1"), "MM-DD-YYYY hh-mm-ss")
End If
End With
End Sub
A couple of points:
Use something other than Worksheet as your variable.
You haven't specified which worksheet Range("A1") is on. That means there's an implicit ActiveSheet, which may not be (is not) the Data sheet.
Now is a date so the IsDate check is unnecessary.
All that said, your revised code might look something like this:
Private Sub Compute_Click()
With ThisWorkbook
Dim ws As Worksheet
Set ws = .Sheets.Add(After:=.Sheets(.Sheets.Count))
ws.Name = Format(Now, "MM-DD-YYYY hh-mm-ss")
End With
End Sub
I am currently attempting to optimize some code. Currently, I use ActiveWorkbook.RefreshAll which slows the whole run time down significantly, in actual fact I only need to refresh two worksheet in the workbook. Does a function such as Worksheet("Name").RefreshAll exist? I am aware of .calculate, but I don't think it will update my pivot tables as well as calculation in the given sheets.
Regards
Why not loop through your workbook, and on those specific sheets, calculate and refresh the pivot table? If the tables aren't on the same sheet, you can tweak the code to loop through pivot tables and refresh that way, instead of within the "each sheet" loop.
Sub t()
Dim ws As Worksheet
Dim pvt As PivotTable
For Each ws In ActiveWorkbook.Worksheets
If ws.Name = "Sheet4" Or ws.Name = "Sheet2" Then
ws.Calculate
For Each pvt In ws.PivotTables
pvt.RefreshTable
Next pvt
End If
Next ws
End Sub
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
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.