How to make data source for pivot table always be the activesheet?

Bee Cee

New Member
Joined
Jan 14, 2021
Messages
12
Office Version
  1. 365
Platform
  1. MacOS
I have a file that is updated daily (each new day is a different tab) on which I've created a pivot table to summarize the data on that particular sheet. I would like to always designate the data location to be the active sheet, since nothing changes. Can anyone help me understand how to do this? Thank you!
 
Hi

See the picture below. I know nothing about the Mac implementation of Excel, but you should look for a module called This Workbook.

The code executes automatically every time a sheet is activated.

event.PNG
 
Upvote 0

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Partial success! When I click on the "Duplicate" button, the new sheet's pivot table updates to the sheet being duplicated, possibly because that script is running before my "duplicate page" script creates a new sheet??
 
Upvote 0
I do not think my code is running if the message box does not appear; note that there are actually two of them.

Where exactly did you paste the code?
 
Upvote 0
I've attached screenshots of where I pasted the code as well as the windows, and the data source once I click "OK" to update.
 

Attachments

  • Screen Shot 2021-02-03 at 1.50.55 PM.png
    Screen Shot 2021-02-03 at 1.50.55 PM.png
    106.5 KB · Views: 5
  • Screen Shot 2021-02-03 at 1.52.26 PM.png
    Screen Shot 2021-02-03 at 1.52.26 PM.png
    88 KB · Views: 5
  • Screen Shot 2021-02-03 at 1.54.07 PM.png
    Screen Shot 2021-02-03 at 1.54.07 PM.png
    225.9 KB · Views: 5
  • Screen Shot 2021-02-03.jpg
    Screen Shot 2021-02-03.jpg
    138.6 KB · Views: 5
Upvote 0
From left to right:

  • This is a message box
  • This is the other message box
  • According to your worksheet layout, is this range correct?
  • The code seems to be in the right place
Is it working in the way you want?
 
Upvote 0
The Screen shots uploaded out of order, the first message box asks to update, the second says updated. After the update, the data source is not the active sheet as originally desired, but the page just before the active sheet.
 
Upvote 0
I wonder if I understood your request correctly… the code is doing what I expect.

Try the version below which will give more information about what is going on.

The code should automatically run every time a sheet is activated.

VBA Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim Data_sht As Worksheet, ps As Worksheet, pn$
If MsgBox("Update pivot table?", vbYesNo) = vbNo Then Exit Sub
Set Data_sht = Sh
Set ps = ThisWorkbook.Worksheets("first")
MsgBox "Active sheet is " & Sh.Name
pn = "PivotTable1"
ps.PivotTables(pn).ChangePivotCache ThisWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=Data_sht.Name & "!" & _
Data_sht.[a1].CurrentRegion.Address(ReferenceStyle:=xlR1C1))
ps.PivotTables(pn).RefreshTable
MsgBox pn & "'s data source range has been updated!" & vbLf & _
"Source is " & ps.PivotTables(pn).SourceData
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,375
Messages
6,119,165
Members
448,870
Latest member
max_pedreira

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