+ Reply to Thread
Results 1 to 4 of 4

Thread: Faster code for selecting dates from conditions, in pivot table

  1. #1
    Registered User
    Join Date
    01-13-2011
    Location
    scandinavia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Faster code for selecting dates from conditions, in pivot table

    Hi,

    I have a common issue with selecting the wanted items (dates) in a pivot table. The condition for selection is simple: select all dates between a start-date and an end-date which these two dates are coming from cetain cells on an another sheet.

    I have the following, probably well-known procedure: For each pivot item in the pivot table-> if the item has value later/earlier than enddate/startdate then don't select the item, and otherwise select it:

    Sub showitem()
          
        Worksheets("pivot").PivotTables("Pivottable1").ClearAllFilters
        Dim pt As PivotTable
        Dim pf As PivotField
        Dim pi As PivotItem
        Dim startdate As Date
        Dim enddate As Date
        
        Set pt = Worksheets("pivot").PivotTables("Pivottable1")
        Set pf = pt.PivotFields("Date")
    
        startdate = CDate(Worksheets("Master").Range("D3"))
        enddate = CDate(Worksheets("Master").Range("E3"))
        
        On Error Resume Next 
        'Worksheets("pivot").Activate
        
       With pf
                For Each pi In pf.PivotItems
                If CDate(pi.Value) < startdate Or CDate(pi.Value) > enddate Then
                    pi.Visible = False
                   Else
                    pi.Visible = True
                   End If
            Next pi
                   
        End With
        
    End Sub
    This works very fine! But i wondered if it could be made faster, because I actually have to wait almost 20 seconds to run the whole for-statement. Because the code applies the condition at every single pivot-item in the pivot-field "Date" and that's not cool I think.

    I wondered if one can make two bounds: upper=enddate and lower=startdate and then only let the for-loop run for the items in between and then say that all the rest of date-items should not be selected.

    Does any have an idea how to write such a code? or any other idea is very welcomed! Thanks in advance!
    Last edited by Cedicon; 07-26-2011 at 06:55 PM.

  2. #2
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,229

    Re: Faster code for selecting dates from conditions, in pivot table

    You could try:

    Sub showitem
    Application.ScreenUpdating = False
    Application.Calculation=xlCalculationManual
    :
    your code
    :
    Application.Calculation=xlCalculationAutomatic
    Application.ScreenUpdating = True
    End Sub

    Regards

  3. #3
    Registered User
    Join Date
    01-13-2011
    Location
    scandinavia
    MS-Off Ver
    Excel 2007
    Posts
    26

    Re: Faster code for selecting dates from conditions, in pivot table

    Quote Originally Posted by TMShucks View Post
    You could try:

    Sub showitem
    Application.ScreenUpdating = False
    Application.Calculation=xlCalculationManual
    :
    your code
    :
    Application.Calculation=xlCalculationAutomatic
    Application.ScreenUpdating = True
    End Sub

    Regards
    Fancy! it took only 4 seconds instead of 22! ..many thanks and i'm glad that it only needed that. after a surf on the net after you replied I found out this is quite common to use and i have also seen it before but never gave it enough attention. But thanks now i know how to speed up codes

  4. #4
    Forum Guru TMShucks's Avatar
    Join Date
    07-15-2010
    Location
    Manchester, England
    MS-Off Ver
    MSO 2003 & 2007
    Posts
    6,229

    Re: Faster code for selecting dates from conditions, in pivot table

    You're welcome.

+ 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