Is there a “sister” function to “AutoFilter” in Excel Interop that works for columns instead of rows?

◇◆丶佛笑我妖孽 提交于 2019-12-02 09:43:48
B. Clay Shannon

I don't think there is such functionality, but similar can be achieved using PivotTables.

I still have work to do on getting it all to look decent, but I did find out how to create a column filter, based on an answer here.

With the following code:

var pch = _xlBook.PivotCaches();
// TODO: Make range dynamic
Range sourceData = _xlBook.Worksheets["PivotData"].Range["A1:G318"];

PivotCache pc = pch.Create(XlPivotTableSourceType.xlDatabase, sourceData);
PivotTable pvt = pc.CreatePivotTable(_xlPivotTableSheet.Range["A8"], "PivotTable");

pvt.PivotFields("Description").Orientation = XlPivotFieldOrientation.xlRowField;
pvt.PivotFields("MonthYr").Orientation = XlPivotFieldOrientation.xlColumnField;

...I can take the source data from one sheet ("PivotData") and slap it on another sheet (_xlPivotTableSheet). The part that gives me the "filter a set of columns" functionality is the last line above. That leaves me with the following:

Though you cannot see it, there is a column "201509" as well as the "201510", and they can be filtered to display or not, individually or collectively.

Now to get the rest of the data to display as it should...

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