Trouble Creating Two Seperate Pivot Caches

mattc7786

New Member
Joined
Oct 1, 2018
Messages
4
Hi team,

I am new to Excel Macros and VBA. I am working on a report summary and have managed to create a code that will create a pivot table and filter it. I have another sheet in the same workbook that I want to do the same thing with. When I use the following code and change the names appropriately it still is using the same pivot cache from the other sheet. I need it to make a new pivot cache. What is the best way to do that with what I have below?

Thanks,

Matt




Sub CreatingAndEditingPivotTable2()


Dim pc As PivotCache
Dim pt As PivotTable
Dim pf As PivotField

If ThisWorkbook.PivotCaches.Count = 0 Then
Set pc = ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=wsKWH.Name & "!" & wsKWH.Range("A1").CurrentRegion.Address, _
Version:=xlPivotTableVersion15)

Else
Set pc = ThisWorkbook.PivotCaches(1)
End If

Worksheets.Add
Range("A3").Select

Set pt = pc.CreatePivotTable( _
TableDestination:=ActiveCell, _
TableName:="KWHPivot")

Set pf = pt.PivotFields("KWH Diff")
pf.Orientation = xlDataField
Set pf = pt.PivotFields("Curr. KWH")
pf.Orientation = xlDataField
Set pf = pt.PivotFields("Prv. KWH")
pf.Orientation = xlDataField

Set pf = pt.PivotFields("Cust ID")
pf.Orientation = xlRowField

Set pf = pt.PivotFields("Market")
pf.Orientation = xlPageField

ActiveSheet.Name = "KWH Summary 1000+"

End Sub

[/code]
 

mattc7786

New Member
Joined
Oct 1, 2018
Messages
4
I have removed this part of the code

Else
Set pc = ThisWorkbook.PivotCaches(1)

[/code]

I thought maybe this was my problem but now I am getting a different error and the pivot table isn't even being created. I am getting a Run-time error '424': Object required.

It is then highlight the portion of the code that I removed the above piece from.
 

mattc7786

New Member
Joined
Oct 1, 2018
Messages
4
I have figured it out. I was a bit too fancy with my code and started from scratch and got it to work.
 

Forum statistics

Threads
1,082,153
Messages
5,363,459
Members
400,736
Latest member
vipamuk

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top