I am trying to find a way to display PivotTable Data Source in excel worksheet. I know we can manually view the Data source by clicking on PivotTable Tools Analyze > Change Data Source > Change Data source to display source in a separate window. I would like to know if it is possible dynamically display the source value as text in a linked cell(sample cell F4 below).
It can be one way relationship, meaning changing PivotTable Data Source, the display cell should be automatically updated, but changing the cell value in display cell does not necessary lead to change in PivotTable Data source. (**but if it change be done for both directions, would be even better.)
welcome both VBA and non-VBA solutions.
Any suggestions are much appreciated.
THANK YOU VERY MUCH!
Create this function.
Function PivotTableSource(myPivot As String) As String
Dim rawSource As String
Dim a1Source As String
Dim bracket As Long
Application.Volatile
rawSource = ActiveSheet.PivotTables(myPivot).SourceData
a1Source = Application.ConvertFormula(rawSource, xlR1C1, xlA1)
bracket = InStr(1, a1Source, "]")
PivotTableSource = "=" & Mid(a1Source, bracket + 1)
End Function
Then put =PivotTableSource("PivotTable1") in cell F4. PivotTable1 is the default name of the first Pivot Table created in a spreadsheet. Change it if your pivot table is named something else.
It would be interesting to look at the events associated to the PivotTable Data Source, in the Microsoft documentation for example.
If there is an event triggered on the Data Source change, you can create a handler to update the value in the F4 cell.
Recording a macro while changing the Data Source of the PivotTable might also give some interesting hints.
Related
I have created a pivot table using the data in another sheet. My pivot table is working fine. When ever there is a data change when I refresh the table its captured correctly.
I tried to access the source data details for this pivot table using vba. By using the following code
Set PT = Worksheets("PivotTable").PivotTables("PivotTable1")
Debug.Print PT.SourceData
It gave me the result
Data!C1:C6
It gave the sheet name properly but not the data range. Where am I going wrong.
Is there any other alternative available to get the source data details.
Thanks in advance.
You're pulling the R1C1 reference. That can be changed with application.convertformula. Note that because you're using the entire column, it would make sense to reduce the scope of range within the sheet's usedrange area.See below how it would work.
Set PT = Worksheets("PivotTable").PivotTables("PivotTable1")
Set rngSourse = Range(Application.ConvertFormula(pt.SourceData, xlR1C1, xlA1))
debug.print rngSourse.address
Set WSsource = rngSourse.Worksheet
Set rngSourse = Intersect(WSsource.UsedRange, rngSourse) 'now it's only the used
debug.print rngSourse.address
I am trying to create auto generated charts in excel. I need to create 50 charts originated from one excel table. There is an unique id for each chart.
What I am looking for is when I click the Unique id in Column B(e.g. ASD) the chart will pop up displaying the Sections(column) Vs. Year for the selected unique id. Below I have pasted an example chart for convenience. I am not good at programming. I really need your help. I will really appreciate if you please help me out here. Thanks
Example Graph of Number OF TEACHER VS CLASSES & FEE
Example Table for Creating Graph
I tend to use a helper sheet, I place in it the formulas to gather the data required to be used in a chart typically using SUMIFS(), COUNTIFS(), VLOOKUP(), etc.
Alternatively if I'm doing a larger BI spreadsheet I will use ListObjects (tables) and define names in the Name Manager using a combination of OFFSET, MATCH, VLOOKUP, etc to automatically return a range of values from the ListOjects and then reference these in the chart/s.
Whichever of the two above options you use we can update the target Unique ID by using the following VBA, note the workbook will need to be saved as a Macro Enabled Workbook. This code will update Cell A1 in the HelperSheet which we will use to update the data for the chart;
Go to VBA (F11)
Open the code base for the sheet which holds your IDs.
Add the below code.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim varIntersects As Variant
'Change Columns(1) to match your ID column.
Set varIntersects = Intersect(Selection, ActiveSheet.Columns(1).EntireColumn)
If Application.Selection.Cells.Count = 1 Then
If Not varIntersects Is Nothing Then
If Not Selection.Value = "" Then
'Change HelperSheet and A1 to suit your needs.
Worksheets("HelperSheet").Range("A1") = Selection.Cells.Value
End If
End If
End If
End Sub
I have an Excel Workbook with a Sheet that contains some company template headers with a Pivot Table placed underneath.
This document is to be used as a template for pivot reports and the headers have to remain intact above the Pivot Table however when items are added to the Page Filters, the Pivot Table tries to expand upwards and ends up overwriting the header rows above.
I need the Pivot Table to expand down the page instead of up the page when Report Filters are added.
After looking to see if there are and settings that can be changed to make the pivot table expand down the page I looked into how the functionality could be scripted using VBA. Adding the below code to the Worksheet via the Visual Basic for Applications screen checks the location of the PivotTable on each update and then moves it up or down the page if Page Filters are added or removed from the report. Setting the StartLineLet & StartLineNum variables to the Cell Address that you want the top left corner of the pivot table or page items to remain at.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Dim StartLineLet As String
Dim StartLineNum As Integer
Dim DataAddr() As String
Dim PageAddr() As String
StartLineLet = "B"
StartLineNum = 6
DataAddr = Split(Target.TableRange1.Cells(1).Address, "$")
PageAddr = Split(Target.TableRange2.Cells(1).Address, "$")
If PageAddr(2) <> StartLineNum Then
Target.Location = "$" & StartLineLet & "$" & (DataAddr(2) + (StartLineNum - PageAddr(2)))
End If
End Sub
You can also instruct the PivotTable (via PivotTable Options > Layout & Format) to display the report fields Down, Then Over by changing the Report filter fields per column, e.g. from zero to the value of three.
This will move the PivotTable with a limited amount of three rows down, and consequently start adding Report Fields to the right (in blocks of three rows)
I have 2 lists in excel. First one is for searching (i want to have dropboxes), and second list is for data.
In second list I have filtered data. But what I want to do now is filter from parameters given in first list.
How can I transfer filter headers on first page?
I want to select brand on 'Search' list and results will be filtered on 'Rows' list.
I can't think of a way to do this exact thing without VBA. Certainly would love to know if there is a way, so maybe someone else can chime in.
That said, here is a small VBA procedure that will get what you want. It works based off a change in the drop down box for Brand in your Search sheet. Follow steps below to implement:
once in Excel hit Ctrl + F11 on your keyboard. This opens up the VBE
In the Project - VBAProject window in the upper left click the Object referring to the Search sheet
Paste the below code into the big window on the right referring to that sheet.
Make sure to save the file as an .xlsm file (Excel-Macro Enabled File) if using XL2007 or greater.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim wksFilter As Worksheet, wks As Worksheet
Dim rngFilter As Range
'replace "A6" with the cell where the Brand dropdown is
If Target.Address = "$A$6" Then
Set wks = Sheets(Target.Parent.Name)
Set wksFilter = Sheets("Rows")
'may need to adjust the number 1 to match the exact location of your Search Column in the rows sheet
wksFilter.UsedRange.AutoFilter 1, wks.Range(Target.Address)
End If
End Sub
I have a set of Pivot tables/charts automatically created from VBA code with custom formatting.
When I change the selection of data to be displayed from the drop down list in the chart, the custom formatting reverts back to the chart type's default display. The underlying chart data is not changed, only what's displayed on the chart. What do I have to do to retain that custom formatting everytime I change the pivot item selection in a row field?
Edit 1: The underlying data for the pivot table does not change. However, everytime I change a filter on a row, column or page field, the updated pivot chart loses the original format set by the VBA code.
Edit 2: This only pertains to Excel versions older than Excel 2007.
What it sounds like to me is that the VBA code generates the pivot table/chart and then you are trying to tweak it afterwards. Unfortunately with VBA, when you update the pivot table/chart, all your custom formatting is lost. Excel is basically creating a new one each time as well.
If you did the formatting by hand in Excel, I think Excel keeps track of that and will keep your formatting as you change the data. The example I can think of is if you accidentally delete a sheet you an Undo it, however if you delete a sheet in VBA there's no possible way to Undo.
I would try this as a solution.
Create two procedures: one that creates a PivotTable from scratch when given data, and the other that formats a pivot table when given the range of a pivot table. The code could look something like this
Sub GeneratePivotTable(rng as Range, var1 as Variant, var2 as Variant)
' Code here that makes your Pivot Table
' pass whatever you need to get the job done
End Sub
Sub FormatPivotTable(rng as Range, var1 as Variant, var2 as Variant)
' Code here to format pivot table given range and other information
' You might even be able to just directly pass a PivotTable object :D
End Sub
Then all you would have to do next to make it functional like your current code is create a wrapper function that just calls both.
Sub GenerateAndFormatPivotTable(rng as Range, var1 as Variant, var2 as Variant)
Call GeneratePivotTable(rng, var1, var2)
' Maybe some processing here to set up the next call
Call FormatPivotTable(rng, var1, var2)
End Sub
If you wanted to pass the PivotTable as an object you could do something neat like....
Function GeneratePivotTable(variables as Variant) as PivotTable
' Generate your pivot table and return it
GeneratePivotTable = myPivotTableThatIMade
End Function
Sub FormatPivotTable(aPivotTableThatYouMake as PivotTable)
' Code that formats your PivotTable
End Sub
Sub GenerateAndFormatPivotTable(rng as Range, var1 as Variant, var2 as Variant
Call FormatPivotTable(GeneratePivotTable(variables as Variant)
End Sub
You do all this so if you make a table, then have to tweak it, you can use that FormatPivotTable to format the updated table.
For extra madness, have the sheet that has the PivotTable fire it's Worksheet.Change or Worksheet.Activate event to search for a PivotTable and call your formatting function :D