Pivot table to select activeworkbook

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,589
Office Version
  1. 2021
Platform
  1. Windows
I have the following code below which works perfectly to update the Pivot tables




Code:
 Sub Update_Pivot()
Sheets("Pivot Table").Select


    ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "C:\My Documents\[Mens Dept Sales.xlsm]Imported Data!C1:C19" _
        , Version:=xlPivotTableVersion14)
   With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Ageing")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
        "PivotTable4").PivotFields("Outstanding"), "Count of Outstanding", xlCount
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Count of Outstanding")
        .Caption = "Sum of Outstanding"
        .Function = xlSum
    End With
    ActiveSheet.PivotTables("PivotTable4").PivotSelect "Ageing[All]", xlLabelOnly _
        + xlFirstRow, True
    Selection.Group Start:=30, End:=120, By:=30
    ActiveSheet.PivotTables("PivotTable4").PivotSelect "Ageing[All]", xlLabelOnly _
        + xlFirstRow, True
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .PivotItems("(blank)").Visible = False
    End With
    Range("A5").Select
     With Range("A:E").EntireColumn.AutoFit
         End With
End Sub


I would like to amend the section below so that instead of having to type the name each time the workbook is saved using another name, the active workbook (being this workbook) is displayed over here using code to extract the name from the workbook

Code:
 "C:\My Documents\[Mens Dept Sales.xlsm]Imported Data!C1:C19" _


I have attempted to do this but cannot get it to work

It would be appreciated if someone could assist me



Code:
 Sub Update_Pivot()
Sheets("Pivot Table").Select
Dim wkb     As Workbook
   
                
          ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        Set wkb = activeworkbook.Sheets(Imported Data!C1:C19") _
        , Version:=xlPivotTableVersion14)
   With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .Orientation = xlColumnField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Ageing")
        .Orientation = xlRowField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
        "PivotTable4").PivotFields("Outstanding"), "Count of Outstanding", xlCount
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Count of Outstanding")
        .Caption = "Sum of Outstanding"
        .Function = xlSum
    End With
    ActiveSheet.PivotTables("PivotTable4").PivotSelect "Ageing[All]", xlLabelOnly _
        + xlFirstRow, True
    Selection.Group Start:=30, End:=120, By:=30
    ActiveSheet.PivotTables("PivotTable4").PivotSelect "Ageing[All]", xlLabelOnly _
        + xlFirstRow, True
    With ActiveSheet.PivotTables("PivotTable4").PivotFields("Name")
        .PivotItems("(blank)").Visible = False
    End With
    Range("A5").Select
     With Range("A:E").EntireColumn.AutoFit
         End With
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
If the workbook that pivot is in and the sheet that acts as its source data are in same file, then it shouldn't be necessary to use filename and path in source.
Code:
ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        [COLOR=#0000ff]"Imported Data!C1:C19"[/COLOR] _
        , Version:=xlPivotTableVersion14)
But if you need the path and name, then this should work:
Code:
ActiveSheet.PivotTables("PivotTable4").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
       [COLOR=#0000ff] Thisworkbook.Path & "\[" & Thisworkbook.Name & "]Imported Data!C1:C19" [/COLOR]_
        , Version:=xlPivotTableVersion14)
 
Upvote 0
Thanks for your input. I used your first section of code, which worked perfectly
 
Upvote 0

Forum statistics

Threads
1,223,238
Messages
6,170,939
Members
452,368
Latest member
jayp2104

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
Back
Top