VBA inserted pivot table field and values columns both not showing

牧云@^-^@ 提交于 2020-12-15 05:42:25

问题


I wanted to display a field and its values column so I can filter the field column within a pivot table.

I first used the record macro option because I don't know VBA very well. Then I cleaned it up. Here is the actions I recorded:

  • Remove a field ("Present") from my pivot table
  • add a field ("Week") to my pivot table
  • add that same field to the area VALUES creating another column called "Count of Week"
  • Changed "Count of week" to "Sum of week"
  • hide column that "Sum of Week" appears in
  • Filter the field "Week" for <11

End recording

I don't know a lot about VBA but I'm learning through cleaning up the recordings I do. Here is what the code looks like (after some cleaning up):

Sub NewHires()
    ' NewHires Macro

    Sheets("CrewSheets").PivotTables("PivotTable1").PivotFields("Present").Orientation = _
                                                                                       xlHidden
    With Sheets("CrewSheets").PivotTables("PivotTable1").PivotFields("Week")
        .Orientation = xlRowField
        .Position = 9
    End With
    
    Sheets("CrewSheets").PivotTables("PivotTable1").AddDataField Sheets("CrewSheets").PivotTables( _
        "PivotTable1").PivotFields("Week"), "Sum of Week", xlSum
    
    With Sheets("CrewSheets").PivotTables("PivotTable1").PivotFields("Sum of Week")
        .Caption = "Sum of Week"
    End With
    
    Columns("J:J").EntireColumn.Hidden = True
    
    Sheets("CrewSheets").PivotTables("PivotTable1").PivotFields("Week").PivotFilters.Add2 _
        Type:=xlValueIsLessThan, DataField:=Sheets("CrewSheets").PivotTables("PivotTable1"). _
        PivotFields("Sum of Week"), Value1:=11
        
End Sub

The problem is, the "Week" field is replaced by the "Sum of Week" field and thus won't filter. I have a feeling that it has to do with the names and VBA not seeing a difference between the pivot table field "week" and table value "Sum of week".

If I could just filter the column that the pivot table puts "Sum of Week" in, then that could be a fix. But I can't. I need to have it filter in the pivot table which is exactly what it does when I manually do it.

-UPDATE BELOW-

Before macro:

After Macro

What it should look like

To add clarification, I did not hide column J:J like I normally would, in the "what it should look like" picture.


回答1:


Well, instead of using a macro to move the fields around, I created a slicer for the "week" field and leave it up to the user to filter it down if they need. More or less, it ends with the same result of being able to filter down to <11 if needed.

However, I would like to note that I don't think the solution to what I was trying to do is actually possible. (glitch?)



来源:https://stackoverflow.com/questions/64595824/vba-inserted-pivot-table-field-and-values-columns-both-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!