简体   繁体   中英

Unable to set Pivot Field to Hidden

Hopefully this will be an easy one, but I can't find the answer to this particular scenario. I'm using the following code to hide and unhide pivot fields based on user selections. The variable fieldShow is a boolean that is true if the user wants to see that particular field, while fieldLastVal is a boolean that tells whether that field is currently showing or not.

If fieldShow <> fieldLastVal Then
    If fieldShow Then
        With pt.PivotFields(fieldName)
            .Orientation = xlDataField
            .Function = xlSum
        End With
    Else
        pt.PivotFields(fieldName).Orientation = xlHidden
    End If
End If
fieldLastVal = fieldShow

The issue is on the line to hide the pivot field. The thing is, the code executes just fine. No errors, no nothing. However, it never actually hides the pivot field. It's still there as if nothing happened. I stepped through the code, and that line definitely executes, it just has no effect whatsoever on the pivot table, and I don't understand why.

The line that sets a pivot field's orientation to xlDataField has the desired effect. I'm not really sure what's happening here.

Anythoughts?

Changing a pivot field's Orientation to xlDataField changes its name, eg, from "My Data" to "Count of My Data". So if you are trying to set "Count of My Data" to hidden you might actually be acting on "My Data."

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