I've been debugging this code for hours in creating a pivot table, but unfortunately, I'm not able to figure out the problem. It keeps on displaying, Object doesn't support this property or method.
Sub CreatePivotTable()
Dim sht, srcSheet As Worksheet, ccsheet As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim StartPvt As String
Dim SrcData As String
Dim lrow As Long
Set srcSheet = ThisWorkbook.Sheets("Document Raw")
lrow = srcSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1
Set ccsheet = ThisWorkbook.Sheets("Character Count")
SrcData = srcSheet & "!" & Range("A1:V"& lrow).Address(ReferenceStyle:=xlR1C1)' this is the line that errors
StartPvt = ccsheet & "!" & ccsheet.Range("A79").Address(ReferenceStyle:=xlR1C1)
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="PivotTable1")
End Sub
Any help? Thanks
Try the modified code below (i prefer to setup up the Range, later the PivotCache and PivotTable and this method):
Sub CreatePivotTable()
Dim srcSheet As Worksheet
Dim ccSheet As Worksheet
Dim pvtCache As PivotCache
Dim pvtTbl As PivotTable
Dim StartPvt As Range
Dim SrcData As Variant
Dim lrow As Long
Set srcSheet = ThisWorkbook.Sheets("Document Raw")
lrow = srcSheet.Cells(srcSheet.Rows.Count, 1).End(xlUp).Row
Set ccSheet = ThisWorkbook.Sheets("Character Count")
' make sure you have valid data from Column A to Column V
Set SrcData = srcSheet.Range("A1:V" & lrow)
Set StartPvt = ccSheet.Range("A79")
'Create Pivot table from Pivot Cache
Set pvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, SrcData)
' add this line in case the Pivot table doesn't exit >> first time running this Macro
On Error Resume Next
Set pvtTbl = ccSheet.PivotTables("PivotTable1") ' check if "PivotTable1" Pivot Table already created (in past runs of this Macro)
On Error GoTo 0
If pvtTbl Is Nothing Then
Set pvtTbl = ccSheet.PivotTables.Add(PivotCache:=pvtCache, TableDestination:=StartPvt, TableName:="PivotTable1")
Else
pvtTbl.ChangePivotCache pvtCache
pvtTbl.RefreshTable
End If
End Sub
Related
I keep having the same error "13" "type incompatibility" while doing a pivot table.
Sub tracer()
Dim cache As PivotCache
Dim table As PivotTable
Dim prange As range
Dim lastrow As Long
Dim lastcol As Long
lastrow = data.Cells(data.Rows.count, "A").End(xlUp).Row
lastcol = 15
Set prange = data.Cells(1, 1).Resize(lastrow, lastcol)
graphe_clos.Select
Set cache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=prange)
Set table = cache.CreatePivotTable(TableDestination:=graphe_clos.Cells(1, 1), TableName:="Terminator")
End Sub
Thank you for Help
The safer way to assign SourceData to your PivotCache is by using the following line
Set Cache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pRange.Address(False, False, xlA1, xlExternal))
adding the fourth parameter xlExternal verifies that the range is also pulling the worksheet's name.
See more explanation inside the code's comments.
Modified Code
Sub tracer()
Dim Cache As PivotCache
Dim Table As PivotTable
Dim pRange As Range
Dim LastRow As Long
Dim LastCol As Long
' use With statement to shorten and fully qualify all your nested objects
With Data
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
LastCol = 15
' set the Range of the Pivot's data
Set pRange = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
' graphe_clos.Select '<-- you don't need to "Select" the sheet in order to create the Pivot-Table
End With
' set the Pivot Cache
Set Cache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=pRange.Address(False, False, xlA1, xlExternal))
' set the Pivot Table
Set Table = Cache.CreatePivotTable(TableDestination:=graphe_clos.Cells(1, 1), TableName:="Terminator")
End Sub
Regarding your latest question, modify:
With ActiveSheet.PivotTables("Terminator").PivotFields("Evt F")
With
With Table.PivotFields("Evt F")
since you already set your PivotTable object to Table.
Sub TestingMacros()
'
' TestingMacros Macro
'
Dim sht As Worksheet
Dim SrcData As String
Dim pvtCache As PivotCache
Sheets("Sheet1").Select
'Determine the data range you want to pivot
Set sht = ThisWorkbook.Worksheets("Sheet1")
SrcData = sht.Name & "!" & Range("A1:R23").Address(ReferenceStyle:=xlR1C1)
'Create New Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
'Change which Pivot Cache the Pivot Table is referring to
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache (pvtCache)
End Sub
This last sentence (ActiveSheet.PivotTables("PivotTable1").ChangePivotCache (pvtCache)) has error 438. Cannot figure why.
Try the code below, explanations inside the code's comments:
Option Explicit
Sub TestingMacros()
' TestingMacros Macro '
Dim sht As Worksheet
Dim SrcRng As Range
Dim SrcData As String
Dim pvtCache As PivotCache
Dim pvtTbl As PivotCache
' Set the sheet obhect for the Pivot-Table data
Set sht = ThisWorkbook.Worksheets("Sheet1")
' set the data range for the Pivot-Table
Set SrcRng = sht.Range("A1:R23")
SrcData = SrcRng.Address(False, False, xlA1, xlExternal) ' get the Address string with sheet reference
' Create New Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
' set the Pivot-Table object
Set pvtTbl = sht.PivotTables("PivotTable1")
' Change which Pivot Cache the Pivot Table is referring to
pvtTbl.ChangePivotCache pvtCache
' refresh the Pivot-Table's data
pvtTbl.RefreshTable
End Sub
This worked for me:
Sub TestingMacros()
Dim sht As Worksheet
Dim SrcData As Range
Set sht = ThisWorkbook.Worksheets("Sheet1")
Set SrcData = sht.Range("A1:R23")
sht.PivotTables("PivotTable1").ChangePivotCache _
sht.Parent.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=SrcData)
End Sub
I'm changing the data source of pivot table in excel using vba. I have this code below but it's returning a Type mismatch error at this line:
Set pvtcache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase,
SourceData:=DataRange)
Here's my script which I also got from various solutions in google and stackoverflow.
Dim Data_sht As Worksheet
Dim Pivot_sht As Worksheet
Dim StartPoint As Range``
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String
Dim pvtcache As PivotCache
Set Data_sht = wb.Sheets("FINAL_DATA")
Set Pivot_sht = wb.Worksheets("Summary")
PivotName = "PivotTable1"
'Dynamically Retrieve Range Address of Data
Set StartPoint = Data_sht.Range("A1")
Set DataRange = Data_sht.Range(StartPoint,
StartPoint.SpecialCells(xlLastCell))
NewRange = Data_sht.Name & "!" & _
DataRange.Address(RowAbsolute:=True, ColumnAbsolute:=True,
ReferenceStyle:=xlR1C1, External:=True)
'Change Pivot Table Data Source Range Address
Set pvtcache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase,
SourceData:=DataRange)
With Pivot_sht
.PivotTables(PivotName).ChangePivotCache pvtcache
End With
'Refresh Pivot Table
Pivot_sht.PivotTables(PivotName).RefreshTable
'Message
MsgBox PivotName & "'s data source range has been successfully updated!"
Only these lines will successfully create and change the Pivot Cache....
Set pvtcache = ThisWorkbook.PivotCaches.Create(xlDatabase, Data_sht.Name & "!" & Data_sht.Range("A1").CurrentRegion.Address)
Pivot_sht.PivotTables(1).ChangePivotCache pvtcache
Respected Expert, I am trying to execute below VBA code for Define my data and refresh the Pivot Table but I Am getting defined or object defined error-1004. Please guide me where I am doing mistake on below.
Sub Pivot()
Dim shPivot As Worksheet
Dim shData As Worksheet
Dim lr As Long
Dim lc As Long
Dim rng As Range
Set shPivot = ActiveWorkbook.Sheets("Data")
Set shData = ActiveWorkbook.Sheets("Pivot")
lr = shPivot.Range("A" & Rows.Count).End(xlUp).Row
Set rng = shPivot.Range(Cells(1, 1), Cells(lr, 32))
With shData.PivotTables("PivotTable1").PivotCache
.SourceData = rng.Address(True, True, xlR1C1, True)
.Refresh
End With
End Sub
Try the code below:
Option Explicit
Sub Pivot()
Dim shPivot As Worksheet
Dim shData As Worksheet
Dim PT As PivotTable
Dim PTCache As PivotCache
Dim lr As Long
Dim lc As Long
Dim Rng As Range
Set shPivot = ActiveWorkbook.Sheets("Data")
Set shData = ActiveWorkbook.Sheets("Pivot")
With shPivot
lr = .Range("A" & .Rows.Count).End(xlUp).Row
Set Rng = .Range(.Cells(1, 1), .Cells(lr, 32))
End With
' set the Pivot Cache with the updated Data Source
' Set PTCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Rng)
' === Option 2 : set the Pivot Cache with the updated Data Source ===
Set PTCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=shPivot.Name & "!" & Rng.Address(True, True, xlR1C1, False))
' Set the Pivot Table to existing "PivotTable1" in "Data" sheet
Set PT = shData.PivotTables("PivotTable1")
With PT
.ChangePivotCache PTCache
.RefreshTable
End With
End Sub
I am trying to add a pivot table to the same worksheet I am on (the sheet is called holders (corp)) but am having trouble with that.
Sub PivotTable()
Sheets("Sheet2").Select
Dim sht As Worksheet
Dim pvtCache As PivotCache
Dim pvt As PivotTable
Dim pf As PivotField
Dim StartPvt As String
Dim SrcData As String
Dim LastRow As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).row
'Determine the data range you want to pivot
SrcData = ActiveSheet.Name & "!" & Range(Cells(1, "A"), Cells(LastRow,"E")).Address(ReferenceStyle:=xlR1C1)
'Create a new worksheet
Set sht = Sheets("HOLDERS (CORP)")
'Where do you want Pivot Table to start?
StartPvt = sht.Name & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub
I get a debug issue related to the call procedure at the very last 3 lines of code but am not sure why. Help would be much appreciated!
Excel VBA cannot accept R1C1 cell addresses in Range objects.
Change
StartPvt = sht.Name & "!" & sht.Range("A1").Address(ReferenceStyle:=xlR1C1)
To
StartPvt = sht.Range("A1").Address
Then change
Set pvt = pvtCache.CreatePivotTable( _
TableDestination:=StartPvt, _
TableName:="HolderssPivotTable")
End Sub
To
Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), _
TableName:="HolderssPivotTable")
Update - Full refactored code to ensure pivot table ends up on Holders (CORP) sheet.
Sub PivotTable()
Dim sht2 As Worksheet
Set sht2 = Sheets("Sheet2")
With sh2
Dim LastRow As Long
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Determine the data range you want to pivot
Dim SrcData As String
SrcData = .Name & "!" & .Range(.Cells(1, "A"), .Cells(LastRow, "E")).Address
End With
Dim sht As Worksheet
'Create a new worksheet
Set sht = Sheets("HOLDERS (CORP)")
'Where do you want Pivot Table to start?
Dim StartPvt As String
StartPvt = sht.Range("A1").Address
'Create Pivot Cache from Source Data
Dim pvtCache As PivotCache
Set pvtCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Dim pvt As PivotTable
Set pvt = pvtCache.CreatePivotTable(TableDestination:=sht.Range(StartPvt), TableName:="HolderssPivotTable")
End Sub