Consulting

Results 1 to 10 of 10

Thread: Filter Pivot Results In Crash

  1. #1
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254

    Filter Pivot Results In Crash

    HI
    The function GuessAMachine is passed an ItemNumber and a Quantity as strings.
    I modify the Quantity string, clear the pivot filters, and then attempt to filter the pivot using these two strings (ItemNumber and Quantity).
    And it works like a champ if I'm lucky - but more times than not Excel "Stops working" lol. Excel alerts me that it has stopped working and is trying to find a solution. The application closes and then restarts with a new workbook.

    When debugging - excel bails right as I try to filter the pivot table. The best I can guess is that what I'm attempting to filter by is not an item listed in that filter.
    but I'm not entirely sure.

    Should I ( / how do I ) check if the item exists prior to attempting to filter the pivot table?
    Also criticism on my crappy coding will be well received. I'm self taught and I can use all the criticism I can get.

    Function GuessAMachine(ItemNumber As String, Quantity As String)
    'ADD THE REST OF THE QUANTITY STRING FOR FILTERING PURPOSES
         Quantity = Quantity + ".00"
    'SET PIVOT FILTERS TO ITEM OF INTEREST
         'CLEAR ALL FILTERS
              Worksheets("Pivot").Range("A1").PivotTable.ClearAllFilters
         'SET THE FILTERS
              Worksheets("Pivot").Range("A1").PivotTable.PivotFields("Item number").CurrentPage = ItemNumber
              Worksheets("Pivot").Range("A1").PivotTable.PivotFields("Quantity").CurrentPage = Quantity
    'SETUP FOR GUESS
         Sheets("Pivot").Select
         CountOfTAWC = 0
         LastCountOfTAWC = 0
         MyMachineGuess = ""
         MyConfidence = 0
         i = 5
    'FIND MOST LIKELY MACHINE BASED ON HISTORICAL DATA
         Do Until Cells(i, 1).Value = "Grand Total"
              CountOfTAWC = CInt(Worksheets("Pivot").Range("A1").PivotTable.GetPivotData("Count of TA WC", "TA WC", CStr(Cells(i, 1).Value)))
              If CountOfTAWC > MaximumOfCountOfTAWC Then
                   MaximumOfCountOfTAWC = CountOfTAWC
                   MyMachineGuess = CStr(Cells(i, 1).Value)
                   MyConfidence = (MaximumOfCountOfTAWC * 100) / CInt(Worksheets("Pivot").Range("A1").PivotTable.GetPivotData("TA WC"))
              End If
         i = i + 1
         Loop
    'MAKE MACHINE SUGGESTION
         If MyConfidence < 75 Then
              With Worksheets("Settings").Range("Table3")
                   Set C = .Find(CStr(MyMachineGuess), LookIn:=xlValues, LOOKAT:=xlWhole, SEARCHORDER:=xlByRows, MatchCase:=True)
                        If Not C Is Nothing Then
                             GuessAMachine = CStr(C.Offset(0, 1).Value)
                        Else
                             GuessAMachine = MyMachineGuess
                        End If
                   End With
     
         Else
              GuessAMachine = MyMachineGuess
         End If
    End Function
    - I HAVE NO IDEA WHAT I'M DOING

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a sample workbook?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    ... and with some data and the pivot table and the values that don't work
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    Ok, don't laugh at the code - its pretty rough lol
    A change event in "Production Schedule" cell C16 will trigger the failure. (just double click and then click out)
    In contrast, a change event in "Production Schedule" cell C6 will execute without error.
    Attached Files Attached Files
    - I HAVE NO IDEA WHAT I'M DOING

  5. #5
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    Quote Originally Posted by mdmackillop View Post
    Can you post a sample workbook?
    Yes.
    - I HAVE NO IDEA WHAT I'M DOING

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I looked at this but couldn't see the cause. I would try copying and pasting data and code to a new workbook.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    RESOLUTION:
    Attempting to filter my pivot table like this:
    Sheet7.PivotTables("PivotTable1").PivotFields("Quantity").CurrentPage = CStr(Quantity)
    Would cause excel to crash intermittently.

    Turns out the pivot table field held the desired filter 'Quantity' in both a number and a text format.
    I noticed this as I tried to manually filter a known problem causer. (the value appeared twice in the list of available filters)

    My fix: Convert all possible integer values in that data column to an actual number.
    - I HAVE NO IDEA WHAT I'M DOING

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Thanks for posting your solution.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    Created an account solely to thank the OP for posting his solution. It worked for me; I was working on a macro-free worksheet and it crashed every time I tried to de-select "show all" on the lead field. Thank you!

  10. #10
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254
    No doubt - Id be nothing if not for the assistance of the mentors on this site.
    - I HAVE NO IDEA WHAT I'M DOING

Posting Permissions

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