简体   繁体   中英

Is it possible in excel pivot table to apply the report filter only to one column with vba

I have the following dataset:

在此处输入图像描述

Employees from different team are assigned to some tasks and completion rate column shows how much of the assigned task is completed. I want to know the percentage of people in each team who have completed more than half of their task.

In the below image the first pivot table is filtered for items with completion rate >50% . The highlighted data in yellow show the first table data divided by the second table data. I want to get these highlighted data.

在此处输入图像描述

I can get this solution if I add another column to my initial dataset and create a pivot table.

However I am looking for a nicer solution where I won't need an extra column in my initial dataset. I would like to know maybe it is possible to combine these 2 pivot tables into one and then apply the division.

You can set the area of each pivot table, calculate the value for this area, and get the result.

PivotTable Rowrange

The RowRange of the pivot table is as follows.

在此处输入图像描述

PivotTable DataBodyRange

在此处输入图像描述

Code

Sub test()
    Dim pv(1 To 2) As PivotTable
    Dim vRng(1 To 2) As Range
    Dim rngDB As Range
    Dim vDB As Variant, vR() As Variant
    Dim Ws As Worksheet
    Dim r As Long
    Dim i As Long
    
    Set Ws = ActiveSheet
    
    Set pv(1) = Ws.PivotTables(1) 'Check and set the index number of your pivot table.
    Set pv(2) = Ws.PivotTables(2) 'Check and set the index number of your pivot table.
    
    
    Set rngDB = pv(1).RowRange
    vDB = pv(1).RowRange
    'rngDB.Select
    For i = 1 To 2
        Set vRng(i) = pv(i).DataBodyRange
        'vRng(i).Select
        'Stop
    Next i
    
    r = vRng(1).Rows.Count
    ReDim vR(1 To r, 1 To 2)
    For i = 1 To r - 1
        vR(i, 1) = vDB(i + 1, 1)
        vR(i, 2) = vRng(2).Cells(i) / vRng(1).Cells(i)
    Next i
    Range("o2").Resize(r, 2) = vR
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM