получить ParentNodeName из PivotCell в сводной таблице - PullRequest
1 голос
/ 03 апреля 2020

У меня проблемы со следующим UseCase. Когда пользователь щелкает элемент PivotCell, я позже хотел бы знать ParentNode этого элемента.

Вы можете видеть на скриншоте, что выбран «Газы», ​​поэтому теперь я хотел бы получить значение «Инвентаризация», так как это родительский узел этого элемента.

enter image description here

Я до сих пор это использовал, использовал его, чтобы получить indentLevel, но я не могу получить значение ParentNode каким-либо образом.

Sub test()
    Dim sSelection As String
    Dim sPivotName As String: sPivotName = "PT_L1"
    Call declareVariables
    For Each OneCell In Selection
        sSelection = OneCell
    Next OneCell

    Dim oPivot As PivotTable
    Set oPivot = ThisWorkbook.Sheets("Mapping").PivotTables(sPivotName)

    Set oPivotField = oPivot.PivotFields("Account Caption")
    For Each pvtfld In oPivotField.DataRange
        If pvtfld.Cells = sSelection Then
            Debug.Print oPivotField.DataRange.PivotCell.ParentField
        End If
        'Debug.Print pvtfld.IndentLevel

    Next pvtfld
End Sub

Edit : я думаю, что он не может определить parentNode, потому что я нахожусь в другом PivotField в l oop?

Изменить (Таблица разметки):

| Account Caption          | Account Type | L2     | L3                        |
|--------------------------|--------------|--------|---------------------------|
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Cash at bank and in hand | BS           | Assets | Cash and cash equivalents |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Gases                    | BS           | Assets | Inventory                 |
| Resale                   | BS           | Assets | Inventory                 |
| Resale                   | BS           | Assets | Inventory                 |
| Resale                   | BS           | Assets | Inventory                 |
| Resale                   | BS           | Assets | Inventory                 |
| Spares                   | BS           | Assets | Inventory                 |
| Spares                   | BS           | Assets | Inventory                 |
| Spares                   | BS           | Assets | Inventory                 |
| Spares                   | BS           | Assets | Inventory                 |

Любая помощь приветствуется!

1 Ответ

1 голос
/ 03 апреля 2020

Хорошо, поэтому следующее немного неуклюже и WIP. Принимая макет, как показано, вы можете l oop вернуться назад через строки, пока уровень отступа не изменится, а затем взять значение ячейки в этой точке.

Option Explicit

Public Sub test()

    Dim sPivotName As String: sPivotName = "PT_L1"
    Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Mapping")
    Dim oPivot As PivotTable: Set oPivot = ws.PivotTables(sPivotName)

    Dim topRow As Long, currentRow As Long, startLevel As Long, currentColumn As Long
    Dim cross As Variant

    On Error Resume Next
    cross = Intersect(Selection, oPivot.RowRange)
    On Error GoTo 0

    topRow = oPivot.TableRange1.Row + 1
    currentRow = Selection.Row
    currentColumn = Selection.Column
    startLevel = Selection.IndentLevel

    With ws

        If currentRow > topRow And Not IsEmpty(cross) Then

            Do
                currentRow = currentRow - 1
            Loop While .Cells(currentRow, currentColumn).IndentLevel = startLevel And currentRow >= topRow

        End If

        If .Cells(currentRow, currentColumn).IndentLevel < startLevel Then
            Debug.Print .Cells(currentRow, currentColumn).Value
        End If
    End With

End Sub
...