Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default sort in pivot table

my pivot table needs to be sorted by the column "total" . but number of
columns varies, it means the location of the column "total" will be changed
depends on source of data.

can i do that?

dennis


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,979
Default sort in pivot table

The following code will sort by the grand total column:

'================================
Sub SortPivotTotal()

Dim wsPivot As Worksheet
Set wsPivot = Sheets("Pivot")
Dim rngSort As Range
Dim strSort As String
On Error Resume Next
Set rngSort = wsPivot.Cells.Find(What:="Grand Total", _
After:=wsPivot.Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).Offset(1, 0)
On Error GoTo 0
If rngSort Is Nothing Then
MsgBox "No Grand Total found"
Exit Sub
Else
strSort = rngSort.Address(ReferenceStyle:=xlR1C1)
rngSort.Sort Key1:=strSort, Order1:=xlDescending, _
Type:=xlSortValues, OrderCustom:=1, _
Orientation:=xlTopToBottom
End If
End Sub
'================================

Dennis Cheung wrote:
my pivot table needs to be sorted by the column "total" . but number of
columns varies, it means the location of the column "total" will be changed
depends on source of data.



--
Debra Dalgleish
Excel FAQ, Tips & Book List
http://www.contextures.com/tiptech.html

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default sort in pivot table

thx, it helps.

i try to use this code to sort the other field other than grand total, but
it doesn't work.

there are 2 fields in column area, they are category and product.
i need the sub-total of each category and sort by the total of products in
each category.
i tried to use the sort function in tools bar, it can sort only the category
which was selected. i needed to sort each category manully after refresh the
table.
can you help?

dennis

"Debra Dalgleish" wrote in message
...
The following code will sort by the grand total column:

'================================
Sub SortPivotTotal()

Dim wsPivot As Worksheet
Set wsPivot = Sheets("Pivot")
Dim rngSort As Range
Dim strSort As String
On Error Resume Next
Set rngSort = wsPivot.Cells.Find(What:="Grand Total", _
After:=wsPivot.Range("A1"), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, _
SearchFormat:=False).Offset(1, 0)
On Error GoTo 0
If rngSort Is Nothing Then
MsgBox "No Grand Total found"
Exit Sub
Else
strSort = rngSort.Address(ReferenceStyle:=xlR1C1)
rngSort.Sort Key1:=strSort, Order1:=xlDescending, _
Type:=xlSortValues, OrderCustom:=1, _
Orientation:=xlTopToBottom
End If
End Sub
'================================

Dennis Cheung wrote:
my pivot table needs to be sorted by the column "total" . but number of
columns varies, it means the location of the column "total" will be
changed
depends on source of data.



--
Debra Dalgleish
Excel FAQ, Tips & Book List
http://www.contextures.com/tiptech.html



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pivot Table Sort jen2018 Excel Discussion (Misc queries) 1 March 5th 10 05:10 PM
Manual sort in a pivot chart/pivot table Isabel Charts and Charting in Excel 4 November 5th 09 02:48 PM
Pivot Table Sort Erin Searfoss Excel Discussion (Misc queries) 5 May 11th 07 06:01 PM
How do I sort pivot table data outside a pivot table Michael Excel Worksheet Functions 1 January 4th 07 02:45 PM
pivot table sort entries that don't yet appear in table rachael Excel Discussion (Misc queries) 11 September 19th 05 11:29 PM


All times are GMT +1. The time now is 07:33 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"