Automatic update of pivot table- EXCEL - excel

Image error
Dears;
I find a error in bellow code in depurator the VBA point this:
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
The code below:
Sub UpdatePivotTableRange()
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("BASE")
Set Pivot_Sheet = ThisWorkbook.Worksheets("TABELA")
'Enter in Pivot Table Name
PivotName = "Tabela dinâmica3"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("A1")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
'Change Pivot Table Data Source Range Address
Pivot_Sheet.PivotTables(PivotName). _
ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange)
'Ensure Pivot Table is Refreshed
Pivot_Sheet.PivotTables(PivotName).RefreshTable
'Complete Message
Pivot_Sheet.Activate
MsgBox "Relatório " & PivotName & " foi atualizado. Aperte OK."
End Sub

Related

Pivot Table Error | Invalid Call or Procedure

I need some help with this one. I am trying to create a pivot table, starting in Q1, of a range of data in the same sheet. The first if-statement is there because the last column doesn't always contain a header, so I include it there.
I want the range to be dynamic since the size of the tables being made will vary depending on the number of rows of the data in the sheet. The source data is always A1:O & lastRow and the table always needs to be placed in Q1 of the same sheet.
I am getting an error - "Invalid call or procedure" - screenshot below.
The issue line when debugging is where I set the pivot table - also in screenshot below.
If it helps, I am running this code from a separate sheet in a start-up folder.
Any help here would be greatly appreciated!
UPDATED TO SHOW CURRENT CODE USING COMMENT SUGGESTIONS
Sub aaTemp()
'
' aaTemp Macro
'
If Range("O1").Value = "" Then
Range("O1").Value = "Notes"
Else
End If
Dim lastRow, lastColumn As Long
lastRow = ActiveSheet.Range("B65536").End(xlUp).row
lastColumn = 15
'Dim pivotSource As String
'pivotSource = "'" & ActiveSheet.Name & "'!" & Range("A1:O" & lastRow).Address(ReferenceStyle:=xlR1C1)
'Dim pivotDestination As String
'pivotDestination = "'" & ActiveSheet.Name & "'!" & Range("Q1").Address(ReferenceStyle:=xlR1C1)
Dim ws As Worksheet
Dim wb As Workbook
Dim pc As PivotCache
Dim pt As PivotTable
Set ws = ActiveSheet
Set wb = ThisWorkbook
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, _
sourceData:=ActiveSheet.Range("A1:O" & lastRow), _
Version:=xlPivotTableVersion15)
Columns("Q:Z").delete Shift:=xlToLeft
Set pt = ws.PivotTables.Add(PivotCache:=pc, _
TableDestination:=ActiveSheet.Range("Q1"), _
TableName:="PTPivotTable")
End Sub
Sub aaTemp()
'
' aaTemp Macro
'
If Range("O1").Value = "" Then
Range("O1").Value = "Notes"
Else
End If
Dim lastRow, lastColumn As Long
lastRow = ActiveSheet.Range("B65536").End(xlUp).row
lastColumn = 15
Dim ws As Worksheet
Dim wb As Workbook
Dim pc As PivotCache
Dim pt As PivotTable
Set ws = ActiveSheet
Set wb = ActiveWorkbook 'This was the needed change
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, _
sourceData:=ActiveSheet.Range("A1:O" & lastRow), _
Version:=xlPivotTableVersion15)
Columns("Q:Z").delete Shift:=xlToLeft
Set pt = ws.PivotTables.Add(PivotCache:=pc, _
TableDestination:=ActiveSheet.Range("Q1"), _
TableName:="PTPivotTable")
End Sub

Trying to get Macro to automatically adjust DataRange for Pivot Table

I'm sorry if this is basic but I can't seem to figure this out .
Long story short my file has several tabs with lot's of data and each tab has a pivot table. What I need is a Macro that adjusts the data range of the Pivot table to the data in current sheet. I have it now set with a bigger range giving me Blanks which drives me nuts.
Here is my code :
Dim Data_Sheet As Worksheet
Dim Pivot_Sheet As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim LastCol As Long
Dim lastRow As Long
'Set Pivot Table & Source Worksheet
Set Data_Sheet = ThisWorkbook.Worksheets("1")
Set Pivot_Sheet = ThisWorkbook.Worksheets("1")
'Enter in Pivot Table Name
PivotName = "PivotTable1"
'Defining Staring Point & Dynamic Range
Data_Sheet.Activate
Set StartPoint = Data_Sheet.Range("V2")
LastCol = StartPoint.End(xlToRight).Column
DownCell = StartPoint.End(xlDown).Row
Set DataRange = Data_Sheet.Range(StartPoint, Cells(DownCell, LastCol))
NewRange = Data_Sheet.Name & "!" & DataRange.Address(ReferenceStyle:=xlR1C1)
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:=NewRange _
, Version:=6)
Now when I try this I get the "Run time error -2147024809" error stating that The pivottable name is invalid .
This updates all the sheets that have 1 pivot table
Sub Update()
Dim wb As Workbook, ws As Worksheet
Dim rng As Range, cell As Range, newrange As String
Set wb = ActiveWorkbook
For Each ws In wb.Sheets
With ws
If .PivotTables.Count = 1 Then
Set cell = .Range("V2").End(xlToRight).End(xlDown)
Set rng = .Range("V2", cell)
newrange = "'" & ws.Name & "'!" & rng.Address(ReferenceStyle:=xlR1C1)
.PivotTables(1).ChangePivotCache _
wb.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=newrange, Version:=6)
End If
End With
Next
End Sub

Get data pivot table VBA

I have generated a pivot table with the following code.
Sub TCD()
Dim last_row As Integer
last_row = Cells(Rows.Count, 1).End(xlUp).Row
Dim My_range As Range
Set My_range = Range(Cells(1, 1), Cells(last_row, 2))
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim pf_Name As String
'Create a new worksheet
Set sht = Sheets.Add
sht.Name = "TCD"
'Where do you want Pivot Table to start?
StartPvt = "TCD" & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=My_range)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
pvt.PivotFields("Type_c").Orientation = xlRowField
pf_Name = "Max iteration par type"
pvt.AddDataField pvt.PivotFields("Nb_Iteration"), pf_Name, xlMax
'Part of code that does nothing
Dim dbl As Double
dbl = pvt.GetData(Name:="CP")
End Sub
The pivot table looks like this
How can I retrieve the number of each row element ? I have tried using get data (last two lines of code) and getpivotdata. Nothing seems to work.
You can try the following:
dbl = pvt.GetData("'Max iteration par type' CP")

How can I create a dynamic range for a pivot table?

I am trying to write a macro that creates a pivot table on a new sheet based on data from a master spreadsheet. How can I adjust my code to do this?
I've gotten the code to work with static data but whenever the data is dynamic, the macro does not work. I believe the issue is with the first line starting with "srcData = ". The line below, which is commented out, works without any issues.
Option Explicit
Dim pivotSht As Worksheet
Dim dataSht As Worksheet
Dim pCache As PivotCache
Dim pTable As PivotTable
Dim srcData As String
Dim pRange As Range
Dim lastR As Long
Dim lastC As Long
Public Sub buildPivot()
Set dataSht = Worksheets("OOB")
'Defines data range in "OOB" sheet
With dataSht
lastR = .Cells(.Rows.Count, "D").End(xlUp).Row
lastC = .Cells(4, .Columns.Count).End(xlToLeft).Column
Set pRange = .Range(.Cells(1, "D1"), Cells(lastR, lastC))
End With
Sheets("OOB").Activate
srcData = ActiveSheet.Name & "!" & pRange.Address(ReferenceStyle:=xlR1C1)
'srcData = ActiveSheet.Name & "!" & Range("D1:U714").Address(ReferenceStyle:=xlR1C1)
'Delete old "PivotTable" worksheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
'Create new sheet and name it "PivotTable"
Set pivotSht = Sheets.Add
ActiveSheet.Name = "PivotTable"
'Set location of pivot table
startPvt = pivotSht.Name & "!" & pivotSht.Range("A3").Address(ReferenceStyle:=xlR1C1)
'Define pivot cache
Set pCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=srcData)
'Creates pivot table from pivot cache
Set pTable = pCache.CreatePivotTable( _
tabledestination:=startPvt, _
TableName:="Open Order Book Table")
End Sub
The expected output is a pivot table using all data as the source, even with changing data.

Creating Dynamic Range in Pivot Table

I'm trying to change my fixed range (A1:G4193) to one that's dynamic due to the need for new data to be entered on a daily basis.
Here is my code:
Sub Create_Pivot()
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim pf As PivotField
SrcData = ActiveSheet.Name & "!" & Range("A1:G4193").Address(ReferenceStyle:=xlR1C1)
Set sht = Sheets.Add
StartPvt = sht.Name & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
I highly appreciate any help. Thanks!
Assumption - your Pivot Table's dynamic range is changing by the number of rows added (or distracted), while the number of columns stays constant.
Instead of using ActiveSheet, try using referenced objects, like Set SrcSht = Worksheets("Sheet1") and then use that variable.
Try the code below (some of my other modifications are inside the code's notes).
Option Explicit
Sub Create_Pivot_DynamicRange()
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As Range
Dim SrcData As String
Dim pf As PivotField
Dim SrcSht As Worksheet, LastRow As Long
' modify "Sheet1" to your sheet's name
Set SrcSht = Worksheets("Sheet1")
With SrcSht
' find last row with data in Column A (skip blank cells in the middle)
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
' set source data to dynamic number of rows (string)
SrcData = .Name & "!" & .Range("A1:G" & LastRow).Address(ReferenceStyle:=xlR1C1)
End With
Set sht = Sheets.Add
' set the start position directly to a Range (there's no need to use a String as a "middle-man")
Set StartPvt = sht.Range("A1")
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
End Sub
Change your range to a variable - like RNG
The calculate the range , how ever you plan to do it. Last row, last column, last cell address, etc
Then make the code soething like this
Lastrow = ActiveSheet..Range("B" & Worksheets("All_Data").Rows.Count).End(xlUp).Row
RNG = "A1:"G" & Lastrow
SrcData = ActiveSheet.Name & "!" & Range(RNG).Address(ReferenceStyle:=xlR1C1)

Resources