+ Reply to Thread
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24

Thread: VBA Pivot selecting only data

  1. #16
    Registered User
    Join Date
    01-17-2012
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: VBA Pivot selecting only data

    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;

    Sub SortDec()
    '
    ' Macro1 Macro
    ' Macro recorded 31/01/2012 by 533532297
    '
    ActiveSheet.PivotTables(1).PivotField("names") _
    .AutoSort xlDescending, "no. hands"
    '
      
    End Sub
    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.

                   Range("E5").Select
    Range(Selection, Selection.End(xlDown)).Select
       Selection.Sort key1:="R5C5", Order1:=xlDescending, Type:=xlSortValues, _
            OrderCustom:=1, Orientation:=xlTopToBottom
    I have attached a dummy workbook which attempts the former code. Please Advise.

    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.
    Attached Files Attached Files

  2. #17
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: VBA Pivot selecting only data

    It should be PivotFields and not PivotField.
    Good luck.

  3. #18
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: VBA Pivot selecting only data

    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
    Attached Files Attached Files

  4. #19
    Registered User
    Join Date
    01-17-2012
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: VBA Pivot selecting only data

    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
    Attached Files Attached Files

  5. #20
    Registered User
    Join Date
    01-17-2012
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: VBA Pivot selecting only data

    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

  6. #21
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: VBA Pivot selecting only data

    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.

  7. #22
    Forum Guru
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Excel 2010
    Posts
    1,502

    Re: VBA Pivot selecting only data

    Hi AMXl,

    You said
    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 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:
    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.

  8. #23
    Registered User
    Join Date
    01-17-2012
    Location
    London,England
    MS-Off Ver
    Excel 2003
    Posts
    18

    Re: VBA Pivot selecting only data

    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
    Attached Files Attached Files

  9. #24
    Valued Forum Contributor Macdave_19's Avatar
    Join Date
    03-14-2007
    Location
    Nottingham, England
    MS-Off Ver
    12.0
    Posts
    788

    Re: VBA Pivot selecting only data

    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

    =OFFSET(DATA!$A$1,0,0,COUNTA(DATA!$A:$A),COUNTA(DATA!$1:$1))
    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".

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0