John and 0EGO,
Apologies for the belated response, I was a little occupied in implementing your suggestions and neatening my macro. I can safely say that the formula does work!- Many thanks to both of you, it has been causing me a pain for a while.
Just to add to the problem, I do have a couple of other issues I was hoping the two of you (and any other who read this) would help me resolve.
Firstly: having created the pivot tables, I was hoping to rearrange them in descending order. However the issue I am having is the code isn't working. There are, as far as I know, two codes I can try;
The code is giving me an error. The other code is one where I just recorded a macro to establish what the code would be, the issue with this is that it is for a fixed range, therefore it won't cater to a change in the data.Sub SortDec() ' ' Macro1 Macro ' Macro recorded 31/01/2012 by 533532297 ' ActiveSheet.PivotTables(1).PivotField("names") _ .AutoSort xlDescending, "no. hands" ' End Sub
I have attached a dummy workbook which attempts the former code. Please Advise.Range("E5").Select Range(Selection, Selection.End(xlDown)).Select Selection.Sort key1:="R5C5", Order1:=xlDescending, Type:=xlSortValues, _ OrderCustom:=1, Orientation:=xlTopToBottom
The second issues I have is as follows;
I have a Pivot Table, within the pivot item list ("Lowest Ratings") I have options which range from 0 - 21.
I would like to create a code whereby the instructions are; to deselect any items from the pivot item list which are less than 11.
Please Advise!
Many Thanks,
A.
It should be PivotFields and not PivotField.
Good luck.
For your second problem, see if this code helps you get started. It's in the attached workbook as well.Sub Jomili() Dim PT As PivotTable Dim pvtItem As PivotItem ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange).CreatePivotTable TableDestination:="", TableName:= _ "PivotTable1", DefaultVersion:=xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select Set PT = ActiveSheet.PivotTables("PivotTable1") PT.AddFields RowFields:="Region" With ActiveSheet.PivotTables("PivotTable1").PivotFields("Value") .Orientation = xlDataField .Caption = "Sum of Value" .Function = xlSum End With 'Change the pivot table to only give us the info we need For Each pvtItem In PT.PivotFields("Region").PivotItems If pvtItem.Value < 11 Then pvtItem.Visible = False Else pvtItem.Visible = True End If Next End Sub
Hi Rory,
Thanks for your response, I have tried running the code with the suggested correction, I am still receiving error messages.
I have attached the workbook with the correction.
Please advise.
Thanks,
Axl
Hi Jomilli,
Thanks for your response, the code works well. The only issue I have is that I have various pivot tables on the same worksheet. Not all of them have the pivotfields("Region"). therefore I would like the code to specidically operate on the pivottable("AllPivot4").
I made various attemps to do this, but all were unsuccessfull.
Please advise.
Thanks,
Axl
I get no errors with the code in that workbook, it just doesn't do anything, because you need to specify the field caption as it is in the table
ActiveSheet.PivotTables(1).PivotFields("names") _ .AutoSort xlDescending, "Sum of no. hands"
Good luck.
Hi AMXl,
You saidI think you mean "in addition to the PT we just created", right? If so, to work on different PivotTables you just need to define them. See below for untested sample code:I have various pivot tables on the same worksheet. Not all of them have the pivotfields("Region"). therefore I would like the code to specidically operate on the pivottable("AllPivot4").Sub Jomili() Dim PT As PivotTable, PT2 as PivotTable Dim pvtItem As PivotItem ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=ActiveSheet.UsedRange).CreatePivotTable TableDestination:="", TableName:= _ "PivotTable1", DefaultVersion:=xlPivotTableVersion10 ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1) ActiveSheet.Cells(3, 1).Select Set PT = ActiveSheet.PivotTables("PivotTable1") PT.AddFields RowFields:="Region" With ActiveSheet.PivotTables("PivotTable1").PivotFields("Value") .Orientation = xlDataField .Caption = "Sum of Value" .Function = xlSum End With 'Change the pivot table to only give us the info we need For Each pvtItem In PT.PivotFields("Region").PivotItems If pvtItem.Value < 11 Then pvtItem.Visible = False Else pvtItem.Visible = True End If Next 'Work on another pivot table on the same sheet Set PT2 = ActiveSheet.PivotTables("AllPivot4") PT.AddFields RowFields:="Whatever" With ActiveSheet.PivotTables("PivotTable1").PivotFields("Value") .Orientation = xlDataField .Caption = "Sum of Value" .Function = xlSum End With 'Change the pivot table to only give us the info we need For Each pvtItem In PT.PivotFields("Whatever").PivotItems If pvtItem.Value < 11 Then pvtItem.Visible = False Else pvtItem.Visible = True End If Next End Sub.
Hi Jomilli,
I have attached a dummy sheet, the headings for the sheet is exactly as the ones I am using for my actual workbook, but the data is irrelevant.
In the workbook you will find that when you run the macro, there are four pivots being created. I want to use your code "'Change the pivot table to only give us the info we need" specifically for the last two pivots, however when I insert the code it doesn't do that.
Therefore I was wondering if, when inputting the code, we could specify which pivot table to apply it to.
Many Thanks,
Axl
Why Don't you create a Dynamic Named Range and use that as the source:
Click on Cell A1 on the "Data" Sheet then Hit CTRL+F3
and Give the Range a Name i.e. "MYDATA"
Then in the bottom box type this
When your data changes so will the size of the named range, so long as there are no blank column headings in row 1 or blank fields in column 1, if there are use a column that will always contain a value in place of "A".=OFFSET(DATA!$A$1,0,0,COUNTA(DATA!$A:$A),COUNTA(DATA!$1:$1))
Then in your Macro just Reference "MYDATA" in the source
If you want to exclude a few columns off the end then just add -2 to the second counta like so, be sure to give it a new name like "MYDATALESS2":
=OFFSET(DATA!$A$1,0,0,COUNTA(DATA!$A:$A),COUNTA(DATA!$1:$1)-2)
Hope that helps
Last edited by Macdave_19; 02-27-2012 at 11:46 AM.
Mr MaGoo
Magoo.Inc MMVII
If i've helped please add to my Rep by Clicking on the Blue Scales in the top right hand corner of the post
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks