0

I have a PivotTable which helps us review company charging, and I'm trying to figure out how to display comments the employee left in their time charging in a cell on the sheet. The pivot table itself has rows of Projects and Employee Names, and columns for each Month. The values are a sum of hours from the source table ("NameRunSource").

"NameRunSource" has columns for:

  1. Project Charged
  2. Employee Name
  3. Charged Hours
  4. Month
  5. Comments

As well as another couple of columns to help categorize the source of the charging

When I select a data item in the Pivot Table, I want to concatenate the comments associated with those hours, and place them in cell B1 in the sheet.

Project Name Employee Org Hours Month Comments
Project1 Sally Engineering 5 Jan Finished X
Project1 Sally Engineering 5 Feb More text
Project1 Frank Testing 5 Jan Something else
Project2 Frank Testing 5 Feb Don't know
Project2 Sally Engineering 5 Feb the end

I've created the pivot table, and I've got the "on click" macro working.

I can't, however, figure out how to get back to the source table and figure out how to get data out. The code is heavily borrowed from another post to just list out the data that might be available to me from the Pivot Table. And it seems to look good, but I can't turn it into anything.

Public Function GetNameRunComment()
Dim pvtCell As Excel.PivotCell
Dim pvtTable As Excel.PivotTable
Dim pvtField As Excel.PivotField
Dim pvtItem As Excel.PivotItem
Dim pvtParentItem As Excel.PivotField
Dim i As Long
Dim nameRun As ListObject

On Error Resume Next
Set pvtCell = ActiveCell.PivotCell
If Err.Number <> 0 Then
    Debug.Print "The cursor needs to be in a pivot table"
    Exit Function
End If
On Error GoTo 0

If pvtCell.PivotCellType <> xlPivotCellValue Then
    Debug.Print "The cursor needs to be in a Value field cell"
    Exit Function
End If

Set pvtTable = pvtCell.PivotTable
For Each pvtField In pvtTable.PageFields
    i = 0
    For Each pvtItem In pvtField.PivotItems
        If pvtItem.Visible Then
            i = i + 1
            Debug.Print "PageField " & pvtField.Name & " - Pivot Item " & i & " is " & pvtItem.Name
        End If
    Next pvtItem
Next pvtField

Debug.Print "Value Field Name is " & pvtCell.PivotField.Name
Debug.Print "Value Field Source is " & pvtCell.PivotField.SourceName

For i = 1 To pvtCell.RowItems.Count
    Set pvtParentItem = pvtCell.RowItems(i).Parent
    Debug.Print "Row Item " & i & " is " & pvtCell.RowItems(i).Name & ". It's parent Row Field is: " & pvtParentItem.Name
Next i

For i = 1 To pvtCell.ColumnItems.Count
    Set pvtParentItem = pvtCell.ColumnItems(i).Parent
    Debug.Print "Column Item " & i & " is " & pvtCell.ColumnItems(i).Name; ". It's parent Column Field is: " & pvtParentItem.Name
Next i

Debug.Print "Time to get comment"
Set nameRun = ThisWorkbook.Sheets("NameRunSource").ListObjects("NameRunSource")

And I can't really get anywhere else. The results from the printouts are something like:

Value Field Name is [Measures].[Hours]
Value Field Source is [Measures].[Hours]
Row Item 1 is [NameRunSource].[Project].&[Project1]. It's parent Row Field is: [NameRunSource].[Project].[Project]
Row Item 2 is [NameRunSource].[Employee Org].&[Engineering]. It's parent Row Field is: [NameRunSource].[Employee Org].[Employee Org]
Row Item 3 is [NameRunSource].[Name].&[Sally]. It's parent Row Field is: [NameRunSource].[Name].[Name]
Column Item 1 is [NameRunSource].[Month].&[Feb]. It's parent Column Field is: [NameRunSource].[Month].[Month]

Eventually, I want, when you click on the cell in the pivot, for the comments associated with the entries that make up the hours to be concatenated and placed in a cell. I shouldn't have any trouble getting that part done, but I can't figure out how to even get a reference to the cells that have the comments in them based on the location of the click.

Using something like the below seems to work, but I have to imagine there's a better way than the text parsing exercise to peel out "Sally", "Project1", and "Feb" from the Rows and Columns...

Dim theComments As Variant
theComments = Evaluate("=FILTER(NameRunSource[[#Data],[Comments]],(NameRunSource[[#Data],[Name]]=""Sally"")*(NameRunSource[[#Data],[Project]]=""Project1"")*(NameRunSource[[#Data],[Month]]=""Feb""))")

Help!

0 Answers0