I have a Excel Pivot table linked my Word report.
The Pivot table will growth when getting more data. However, when the Pivot table size change, it doesn't be reflected in the Word file. The word file always display the original selected range. So I have to update the link range manually.
Is there is a way to fix this issue or simply the effort?
Many thanks.
Below is my solution to fix this issue.
1. Create a Named Range for the Pivot Table.
2. Change the Linked Range absolute address in Word file to the Named Range
================
Note for 1:
create named range for a Pivot table can be done via Excel "Offset" function.
However, the Offset function is not perfect when there are more data in the same sheet.
so I create my own Excel function to that.
Function PVRange1(Sheet_Name, Pivot_Name) As Range
'Returns a Range object that represents the range containing the entire PivotTable report, but doesn’t include page fields.
'
Dim pvt As PivotTable
Set pvt = Worksheets(Sheet_Name).PivotTables(Pivot_Name)
Set PVRange1 = pvt.TableRange1
End Function
Function PVRange2(Sheet_Name, Pivot_Name) As Range
'Returns a Range object that represents the range containing the entire PivotTable report, including page fields.
Dim pvt As PivotTable
Set pvt = Worksheets(Sheet_Name).PivotTables(Pivot_Name)
Set PVRange2 = pvt.TableRange2
End Function
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 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:
I have the following raw table structure - a list of credit card transactions:
Date | Description | Debits | Credits
Date will be converted using this approach:
Ensure a valid date inside the cell in Excel
Other columns will stay AS IS. I can create the first row of a target table, but I need to expand it downwards to match the number of rows in the source table.
Is there a way to automate the last part, i.e. row expansion?
I am expecting the raw data table to grow over time, so the target table needs to adjust its row count as well (either fully automated or via a single-click macro).
There is a similar question on StackOverflow, but I am using named tables and named columns:
Matching number of rows in one excel table with the number of rows in another
Table names:
Source : TableRawData
Target : ProcessedData
Date conversion formula I am using in the 1st column:
=MorphDate(TableRawData[#Date])
Paste this code into the worksheet with the source Table (ListObject). I used the Table names you specified. You'll need to adjust the worksheet names to the actual ones in your workbook:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim loSource As Excel.ListObject
Dim loTarget As Excel.ListObject
Dim wb As Excel.Workbook
Set wb = ThisWorkbook
With wb
Set loSource = .Worksheets("Source").ListObjects("TableRawData")
Set loTarget = .Worksheets("Target").ListObjects("ProcessedData")
'Only change Target if Source has more rows or columns, i.e,
'don't shrink, only grow.
If loTarget.Range.Rows.Count < loSource.Range.Rows.Count Or _
loTarget.Range.Columns.Count < loSource.Range.Columns.Count Then
With loTarget
'two kinds of 'Resize in one line!
.Resize (.Range.Cells(1).Resize(loSource.Range.Rows.Count, loSource.Range.Columns.Count))
End With
End If
End With
End Sub
As noted in the comments this triggers if either the number of rows or columns gets bigger. If you want it to change if Target grows or shrinks then change the <'s to <>'s.
If you are interested in the two different Resizes used in the code, and some information on copying Tables, see my this post of mine.
I am returning a Excel.range of a listObject (Excel table), based upon the visible rows, using this kind of syntax (where pLO is a list object):
Set returnUniqueList = pLO.range.SpecialCells(xlCellTypeVisible).EntireRow
This is happening AFTER I do an advanced filter on a column. I can see that the advanced filter has worked, and is returning the correct number of rows through visual inspection.
The problem is that the above code snippet returns a range with many areas, which might contain overlapping rows in the areas! So if I iterate through all the areas in the returned range, I get duplicates. How can I either only return the visible rows, or filter out the duplicates during the iteration that follows?
EDIT***************************************************
Erik, some more information regarding the overlapping. the returnUniqueList range above will have numerous 'Area' objects contained within it. These could number from 1 to n, where 'n' can exceed the number of visible rows in my original table.
Each of these Areas is also a range (so could also contain areas 1..n!!!). Looking at the rows in these areas, Area(1) could contain identical rows to Area(2)!
END EDIT************************************************
I can't do an advanced filter to a different range, because I need to return the table worth of filtered table.
I hope this makes sense.
Cheers,
LazzMaTazz
Try same without the .EntireRow as in:
'Set returnUniqueList = pLO.range.SpecialCells(xlCellTypeVisible).EntireRow
Set returnUniqueList = pLO.range.SpecialCells(xlCellTypeVisible)
and see what the Areas are, and if there are yet any overlapping rows in this result.
The solution I've come up with to this problem is to create a new worksheet in my workbook, and copy the filtered table into this new worksheet.
This always (in my testing with various tables so far) seems to copy the filtered table from the source worksheet into contiguous rows in the temporary destination worksheet. This then returns a range object with a single 'area', which can be used reliably.
I have to be careful to:
make sure I clean the temporary worksheet up when I'm finished working with the data
clear the temporary worksheet cells after each operation so old data doesn't cause me problems.
Private Function copyToNewWorkSheet() As Excel.range
' call this when the sourcesheet (pWkSht) is already filtered.
Dim myWkBk As New Excel.Workbook
Dim tempWs As New Excel.Worksheet
' if the first time this is called, create the new worksheet
If WorksheetExists("TempWorkSheet") Then
Set tempWs = pMyWkBk.Worksheets("TempWorkSheet")
Else
Set tempWs = pMyWkBk.Worksheets.Add(After:=pMyWkBk.Worksheets(pMyWkBk.Worksheets.Count))
tempWs.Name = "TempWorkSheet"
End If
' clear the temp worksheet contents
tempWs.Cells.Clear
' reselect my source worksheet (which is already filtered)
pWkSht.Select
' it falls over sometimes if this isn't here - any thoughts???
pWkSht.range("A1", pWkSht.Cells(pWkSht.rows.Count, "A").End(xlUp)).Select
' copy the required from the course worksheet, using information from the table (pLO) on the worksheet
pWkSht.range("A1", pWkSht.Cells(pLO.range.Areas(pLO.range.Areas.Count).rows.Count, "A")).Resize(, pLO.range.Columns.Count).Copy tempWs.range("A1")
' return the 'clean' range from the temporary worksheet
Set copyToNewWorkSheet = tempWs.range("A1", tempWs.Cells(tempWs.rows.Count, "A").End(xlUp)).Resize(, pLO.range.Columns.Count)
End Function
I can upload a workbook if anyone would like to see the solution in full. This problem has taken me a few days to solve - so please feel free to ask!
LazzMaTazz
For instance I have a pivot table on every third worksheet that needs to be edited, on the worksheet is 3 pivot tables, I don't actually know the name of the table but I know it lies on $N$2, is there anyway to get the name or handle of the pivot table from the cell?
The Range object has a PivotTable property that returns the object you want
Set pt = MySheet.Range("N2").PivotTable