VBA - Pivot Tables

MarcusL

New Member
Joined
Sep 3, 2014
Messages
1
Hi All,

I am new to VBA and fighting with it!

1) Why can't I rename a pivot table. The first line of the below code will work but the second line when I try and experimentally rename it, it won't. What is the reason for this:

HTML:
Set PvtTbl = Worksheets("Pivot").PivotTables("PivotTable1")
Set PvtTbl = Worksheets("Pivot").PivotTables("PivotTable50")

2) The second issue I am having is trying to place a filter on pivot table. I can get the line of code to work on all other piovt tables BUT this one. The PivotTable name remains the same so I really don't know why it wont work in this instance. Does anyone know what I am doing wrong. The specific line of code is:

HTML:
PvtTbl.PivotFields("Netting_Id").PivotFilters.Add Type:=xlValueDoesNotEqual, DataField:=PvtTbl.PivotFields("Sum of CVA_EAD"), Value1:=1

and it is part of this sub:

HTML:
Sub PivotTB()

Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim rngData As Range
Dim PvtTblCache As PivotCache
Dim wsPvtTbl As Worksheet
Dim pvtFld As PivotField

Set wsData = Worksheets("UnderlyingData")
Set wsPvtTbl = Worksheets("Pivot")
Set PvtTbl = Worksheets("Pivot").PivotTables("PivotTable1")

For Each PvtTbl In wsPvtTbl.PivotTables
If MsgBox("Delete existing PivotTable!", vbYesNo) = vbYes Then
PvtTbl.TableRange2.Clear
End If
Next PvtTbl
Worksheets("UnderlyingData").Activate
wsData.Range("A3", Range("W3").End(xlDown)).Select
Set rngData = wsData.Range("A3", Range("W3").End(xlDown))
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, _
version:=xlPivotTableVersion11).CreatePivotTable TableDestination:=wsPvtTbl.Range("A1"), _
TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion11
Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1")
PvtTbl.ManualUpdate = True
Set pvtFld = PvtTbl.PivotFields("Internal Legal Entity Cis Code")
pvtFld.Orientation = xlRowField
Set pvtFld = PvtTbl.PivotFields("Netting_Id")
pvtFld.Orientation = xlRowField
Set pvtFld = PvtTbl.PivotFields("CVA_Exemption")
pvtFld.Orientation = xlPageField

With PvtTbl.PivotFields("CVA_EAD")
      .Orientation = xlDataField
      .Function = xlSum
      .NumberFormat = "#,##0"
End With

PvtTbl.PivotFields("Netting_Id").PivotFilters.Add Type:=xlValueDoesNotEqual, DataField:=PvtTbl.PivotFields("Sum of CVA_EAD"), Value1:=1
 
End Sub

Thanks very much for your help!

M
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

Watch MrExcel Video

Forum statistics

Threads
1,130,342
Messages
5,641,592
Members
417,224
Latest member
llama9207

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top