Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XL97 Pivot Table Unable to set the _default property

Status
Not open for further replies.

vaneagle

Technical User
Apr 23, 2003
71
AU
Hi,

I have an issue with some vba code.. when I use VBA on a pivot table it gives me the following message "Unable to set the _default property of the pivotitem class".

It errors on
Code:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Department").CurrentPage = _
        "5"
.

my code is as below:
Code:
Sub Getric()
'
' Getric Macro
' 
'
    Dim week As String
    Dim weekstart As String
    Dim weekend As String
    Dim state As String
    Dim staten As String
    Dim weeksc As String
    Dim weekec As String
    Dim Month As String
    Dim JunkData As New DataObject ' Declare Variable to clear out clipboard
    Dim WB As Workbook
    Dim WB2 As Workbook
    Dim WB3 As Workbook
    
    'Application.ScreenUpdating = False
    Sheets("Menu").Select
    weekstart = Range("e6")
    weekend = Range("e8")
    weekend = weekend + 1
    'weekend = 8
    weeksc = Range("b6")
    weekec = Range("b8")
    Month = Range("e4")
    Set WB2 = Workbooks(Dir(ActiveWorkbook.FullName))
    Debug.Print WB2.FullName
    
    Do While weekstart < weekend
    
    Workbooks.Open FileName:=&quot;U:\Testing\Ric Data\RIC WK&quot; & weekstart & &quot;.xls&quot;, ReadOnly:=True
        
    Set WB3 = Workbooks(Dir(ActiveWorkbook.FullName))
    Debug.Print WB3.FullName
    
    Sheets(&quot;NewSum&quot;).Select
    
    If weeksc = 1 Then
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).RefreshTable
Code:
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;5&quot;
Code:
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a1&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;10&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a18&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;15&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a35&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;20&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a52&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;27&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a67&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        JunkData.SetText &quot;&quot;
        JunkData.PutInClipboard
        ActiveWorkbook.Close SaveChanges:=False
  
    ElseIf weeksc > 1 Then
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).RefreshTable
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;5&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a1&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;10&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a30&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;15&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a59&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;20&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a88&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotFields(&quot;Department&quot;).CurrentPage = _
        &quot;27&quot;
        ActiveSheet.PivotTables(&quot;PivotTable1&quot;).PivotSelect &quot;&quot;, xlDataAndLabel
        Selection.Copy
        WB2.Activate
        Sheets(&quot;Week&quot; & weeksc).Select
        Range(&quot;a113&quot;).Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
        False, Transpose:=False
        WB3.Activate
        JunkData.SetText &quot;&quot;
        JunkData.PutInClipboard
        ActiveWorkbook.Close SaveChanges:=False
    
    End If
    
    weekstart = weekstart + 1
    weeksc = weeksc + 1
    Loop
    
    WB2.Activate
    JunkData.SetText &quot;&quot;
    JunkData.PutInClipboard
    Sheets(&quot;Menu&quot;).Select
    
    ActiveWorkbook.SaveAs FileName:=&quot;U:\Testing\RIC Data &quot; & Month & &quot;.xls&quot;, _
        FileFormat:=xlNormal, Password:=&quot;&quot;, WriteResPassword:=&quot;&quot;, _
        ReadOnlyRecommended:=False, CreateBackup:=False
    Application.DisplayAlerts = True
End Sub

any ideas ??
 
I remember getting an error message like that a long time ago when I was doing something similar. The problem I had was the focus was not on the spreadsheet at the time the VBA run and the pivot table is not recognised. An example of this would that a command button has the focus from clicking, ie selected, and the sheet itself is not active.

To solve my problem I used this statement just before my update to the pivot table :-

ActiveCell.Activate

I hope this solves your problem aswell.

Good luck,
TopJack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top