Populate a Combo Box with Pivot Table Values - excel

I have a combo box with a list of names, that filters a pivot table.
Each name I select changes the values shown at the pivot table.
I need a code that populates a second combo box with those pivot tables values. When I choose a name on the first combo box, the options at the second one will be the ones filtered by that name on the pivot table.
I have done that before in a simpler manner with fixed tables, but now the values are going to change constantly, and that's why I need to use a pivot table for that.

You can use the DataRange of your PivotField as source for your ComboBox.
You may either add each cell's value to the ComboBox items ...
Dim aCell As Range
For Each aCell In ActiveSheet.PivotTables(1).RowFields(1).DataRange.Cells
ActiveSheet.ComboBox1.AddItem aCell.Value
Next aCell
... or the whole ComboBox.List in one by a simple assignment:
ActiveSheet.ComboBox1.List = ActiveSheet.PivotTables(1).RowFields(1).DataRange.Value
I guess you used your "TIMBRA..."-list as the only row field in your pivot table.
If not, you can address the DataRange by names instead, e. g.
ActiveWorkbook.Sheets("...").PivotTables("...").PivotFields("...").DataRange

Related

Display Empty Columns Pivot Table with OLAP Data (Filtered)

I am trying to check values of rows across all columns. My code filters and loop on each category. These data are copied on a certain format to another sheet and will be used as vlookup data. However, there are instances when I change a filter those row data don't have column data. Thus, The column header with no data does not show up in the pivot. I want these columns to show even if filtered. In normal Pivot table, there is "show items with no data" which will do these function. But in OLAP pivot, this option is greyed out. Tried below code but still did not work.
Are there any ways to do this? Appreciate your help on this. Thanks!
Sub DisplayEmptyMembers()
Dim pvtTable As PivotTable
Set pvtTable = ActiveSheet.PivotTables("WeeklySTCperFF")
pvtTable.DisplayEmptyColumn = True
pvtTable.DisplayEmptyRow = True
End Sub

Excel Dropdown to Filter Pivot Tables on Separate Sheet

I've created a drop down using Form Control Combo Box for the user to select/filter the data without having to go to the pivot table itself. How can I connect the Form Control Combo Box through VBA to a pivot table in another sheet so the value selected from the drop down is the filter to be used in the pivot table in the separate worksheet?
I've tried the code below but the pivot table still doesn't catch the text in the drop down. The filter in the pivot table still does not change when I select a different item from the drop down.
Sub Macro34()
Dim valmonth As String
valmonth = Range("D3").Value
Sheets("Pivot_Main").PivotTables("PivotTable6").PivotFields("Month").CurrentPage = _
valmonth
End Sub

Is there a way to find range of a pivot table

Excel 2013. I have pivot table, which grows and shrinks according to the data rows. For example,
1. For the Row label, I have list of all products
2. For the Column label, I have list of manufacturers
3. For the Values, I have a count of 1 for the manufacturer. This would tell me how many manufacturers produces the same product.
Therefore the product list (row) grows and so does the Manufacturer list (columns) expands.
My question: Is there a way to get the range of the pivot, for example if the pivot tables starts at A1 and there are 10 products and 10 manufacturers, then the range of the pivot table would be $A$1:$K$12 (without Total for row or Total for column).
Is there a quick way to extract the range $A$1:$K$12?
Thank you in advance,
G.
To get the pivot table range that doesn't include the filters range, which is usually two rows above the table, use this code. This will select the actual pivot table range, and output the address of the range of the pivot table in a msg box.
Set pt = ActiveSheet.PivotTables(1)
pt.TableRange1.select
Msgbox pt.TableRange1.address
I see you tagged Excel-VBA.
Yes if you are able to consider Range as Range(cells(1,1),cells(12,12)) instead of Range("A1:K12")
There is a way to count field items in VBA.
Rcount = Sheets("YOUR SHEET NAME").PivotTables(1).PivotFields("YOUR ROW NAME").PivotItems.Count
Ccount = Sheets("YOUR SHEET NAME").PivotTables(1).PivotFields("YOUR COL NAME").PivotItems.Count
From there you would need only add 2 to compensate for extra rows/columns
Rcount = Rcount+2
Ccount = Ccount+2
Dim TableRNG as range
Set TableRNG = Range(cells(1,1),cells(Rcount,Ccount))
Hard to go any further without knowing what you are trying to accomplish and im not sure you were really asking for a VBA solution.

Pivot/Table filter activated through link from other table's cell value

I have a list of items to work through and some items require deeper analysis. Essentially I have two tables. The first shows production results and through filtering column U, I get a list of outliers as in the following image.
Table of outliers
The values displayed in column S then require further analysis for which a pivot chart has been set up. In this chart I filter the particular VRTY number to take a closer look.
Pivot Chart for analysis
Both sheets are contained in the same workbook and I essentially work with two windows open, but to go through the list I have to manually enter every single VRTY value in the pivot filter.
The table of the Pivot chart and the outlier table are not related and data sources are different.
In column S of the outlier table (VRTY) I would ideally turn the values into links that automatically set the pivot filter to this value when clicked.
I am a novice at VBA but from the research I've done this will be the only option - I just haven't come by an instructions how to achieve this particular function.
Instructions/ advice would be highly appreciated.
Sambo: Make sure the orientation of the 'Number' field in the PivotChart's underlying PivotTable is a PageField, and then put the below code in the Sheet code module that corresponds to the sheet where the Outliers PivotTable is:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim pf As PivotField
Dim pf_Slave As PivotField
Set pf = ActiveSheet.PivotTables("PivotTable1").PivotFields("VRTY")
Set pf_Slave = ActiveSheet.PivotTables("PivotTable2").PivotFields("number")
If Not Intersect(Target, pf.DataRange) Is Nothing Then
On Error Resume Next
pf_Slave.CurrentPage = CStr(Target)
On Error GoTo 0
End If
End Sub
Change "PivotTable1" and "PivotTable2" as appropriate.
By "Sheet code module" I mean this type of thing:

Get table range in VBA (dynamic)

I have a program in VBA that I am working on that filters values from a table. I'm trying to make this a generic program that works with all tables that you give it. In my program I have to set the range of the table that it is filtering: Set rng = dataSheet.Range("A1:F78"). I was wondering if there was a way to get the Range of a table in excel that has some sort of text value in it so I don't have to specify it in every macro. I guess this is a type of dynamic range.
If there is one cell of your range which is always within your table, like A1 would be always top-left corner of the table. And if there is continuous range of cells until the end of your table you could use .CurrentRegion property in this way:
Set rng = dataSheet.Range("A1").CurrentRegion

Resources