How can I get the members of a PivotTable column field to display in natural order, rather than alphabetically?

与世无争的帅哥 提交于 2019-12-13 15:27:26

问题


I was taught how to convert a date-as-string value that was being converted from what I wanted ("Sep 2015", "Oct 2015" etc.) to what Excel thought it should be ("15-Sep", "15-Oct" etc.) here.

When it was displaying "badly," the columns at least displayed in the right order ("2015-Sep" followed by "2015-Oct"). Now that they are "Sep 15" and "Oct 15", though, they are displaying out of "natural" order and in alphabetical order ("Oct 15" followed by "Sep 15").

This is the too-typical scenario (especially egregiously evident in software development) of the solving of one problem causing another one to rear its ugly rear.

This is how I create the "month" part of the PivotTable:

var monthField = pvt.PivotFields("MonthYr");
monthField.Orientation = XlPivotFieldOrientation.xlColumnField;

Before fixing the display format problem:

After fixing the display format problem ("15-Sep" is now "Sep 15", etc., but the months are now out of order):

Can I "have my cake and eat it, too" so to speak? If so, how?


回答1:


From reading the comments, it sounds like you need to convert your C# date to an excel date. In the second comment you mention that you were able to get values "201509" and "201510" onto an excel sheet.

I suggest you separate the year and month using the LEFT() and RIGHT() functions, then use the DATE() function to get the Excel serial number for 9/1/15 and 10/1/15.

Here's a screenshot of the steps I'm think of (Happy Halloween!):

Finally, you can now format the Serial number using your formula monthField.NumberFormat = "MMM yy". Excel will realize this is a date and sort it chronologically.



来源:https://stackoverflow.com/questions/40050213/how-can-i-get-the-members-of-a-pivottable-column-field-to-display-in-natural-ord

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