+ Reply to Thread
Results 1 to 5 of 5

Pivot Table pagefield returns wrong data when changed with vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-17-2008
    Location
    Vancouver
    MS-Off Ver
    2002 and XP
    Posts
    117

    Pivot Table pagefield returns wrong data when changed with vba

    Hello All,

    Okay, have a combobox populated with salespeople
    you select one and click a "lookup" button. This changes the page fields of multiple pivottables to that salesperson.

    here is the problem. On occasion, the data that is returned actually belongs to a different salesperson. Like if I specify Fred, the page field will be changed to Fred, but when I drill down and have a sheetx with sourcedate, the saleperson will turn out to be Susan.

    wtf excel? I thought we had a deal!

    Any idea what might be causing it?

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Pivot Table pagefield returns wrong data when changed with vba

    It'd be easier to figure out if you'd post a sample workbook, or at least your code.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Contributor
    Join Date
    10-17-2008
    Location
    Vancouver
    MS-Off Ver
    2002 and XP
    Posts
    117

    Re: Pivot Table pagefield returns wrong data when changed with vba

    Here is the code, not sure I can replicate the behaviour in a sample notebook


     
    Dim Salesperson As String
    Dim myrange As Range
    'this is the sales querytable and C is the column with the salespeople.  A salesperson _
    may be listed twice (sales from 2009 and from 2010)
    Set myrange = Sheet1.Range("C:C")
    
    If Sheet8.CL_SalesPersonLookup.Value = "" Then
            Exit Sub
        End If
    Salesperson = Sheet8.CL_SalesPersonLookup.Value
    
        
    Dim isitthere As Boolean
    Dim there As String
    isitthere = True
    On Error Resume Next
        there = Application.WorksheetFunction.Match(Salesperson, myrange, 0)
        If Err.Number <> 0 Then
            isitthere = False
            Else
            isitthere = True
            End If
    Dim Worksheet As Worksheet
    Set Worksheet = ThisWorkbook.Worksheets("Salesperson Lookup Feature")
    
    For Each PivotTable In Worksheet.PivotTables
        With PivotTable
                .PivotCache.MissingItemsLimit = xlMissingItemsNone
                .PivotCache.Refresh
    
                If isitthere = True Then
                    .PivotFields("Salesperson Name").CurrentPage = Salesperson
                    Else
                    If PivotTable.SourceData = "SalesQuery" Then
                        .PivotFields("Salesperson Name").CurrentPage = Salesperson
                        Else
                        .PivotFields("Salesperson Name").CurrentPage = "(All)"
                    End If
                End If
            .RefreshTable
        End With
    Next PivotTable
    
    For Each PivotTable In Worksheet.PivotTables
        With PivotTable
            Dim rng As Range
                If .PivotFields("Salesperson Name").CurrentPage = "(All)" Then
                    For Each rng In .DataBodyRange.Cells
                        rng.Font.ColorIndex = 2
                    Next rng
                    Else
                    For Each rng In .DataBodyRange.Cells
                        rng.Font.ColorIndex = xlAutomatic
                    Next rng
                End If
        End With
    Next PivotTable
    Set Worksheet = Nothing
    
    Dim objCht As ChartObject
    Dim seriescoll As SeriesCollection
    
    Set shtTemp = Sheet8
    Dim i As Integer
    For Each objCht In shtTemp.ChartObjects
            
        With objCht.Chart
            If .PivotLayout.PivotTable.PivotFields("Salesperson Name").CurrentPage = "(All)" Then
                
                 For i = 1 To objCht.Chart.SeriesCollection.Count
                    With objCht.Chart.SeriesCollection(i)
                        .Border.Weight = xlThin
                        .Border.LineStyle = xlNonese
                        .Interior.ColorIndex = 2
                        .Interior.Pattern = xlSolid
                        
                    End With
                Next
            Else
                
                 For i = 1 To objCht.Chart.SeriesCollection.Count
                    With objCht.Chart.SeriesCollection(i)
                        .Border.ColorIndex = 57
                        .Border.Weight = xlThin
                        .Border.LineStyle = xlContinuous
                        .Interior.ColorIndex = xlAutomatic
                        .Interior.Pattern = xlSolid
                    End With
                Next
            End If
        End With
    Next objCht
    
    If isitthere = False Then
        MsgBox "This person has no sales"
    End If
    so what my issue is, is that sometimes, I'll put "John Smith" in the combobox (Sheet8.CL_SalesPersonLookup.Value) and the pivottable page field will say "John Smith" but the data will reflect the sales of "Jennifer Garner". And when I drill down to the raw data (with it creating a new sheet) the salesperson in the column will be "Jennifer Garner"

    it happens sporatically. I first noticed it always happened with a person named with the following syntax: firstname * lastname. Althought I don't think the asterisk does anything to muck up my results. It happens often enough that I don't trust it anymore

  4. #4
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: Pivot Table pagefield returns wrong data when changed with vba

    A couple things:
    1) This code a very difficutl to read for a couple of reasons. You should always declare all your variables at the beginning of the procedure, not throughout the code as you need them. Also, don't name a variable as the variable type (Ex: Dim Worksheet as Worksheet = bad).

    2) I don't believe using the error catcher is the best way to find out if a valid selection was made. Maybe you could skip that altogether by using a listbox to capture all the available salespersons?

    Other than that, I can't find anything that would cause the problem you stated above. Anyone else want to take a crack at it?

  5. #5
    Forum Contributor
    Join Date
    10-17-2008
    Location
    Vancouver
    MS-Off Ver
    2002 and XP
    Posts
    117

    Re: Pivot Table pagefield returns wrong data when changed with vba

    Thank you,

    the error catcher is because some of the pivottables reference a query that would have all the salespeople in it (distribution of quota across provinces) and other tables reference sales (the error checker is for those items dependent on sales)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1