Results 1 to 5 of 5

Changing a pivot table with a cell value

Threaded View

Coldsteel Changing a pivot table with a... 09-15-2010, 11:39 PM
DonkeyOte Re: Changing a pivot table... 09-16-2010, 02:13 AM
Coldsteel Re: Changing a pivot table... 09-16-2010, 03:16 AM
DonkeyOte Re: Changing a pivot table... 09-16-2010, 03:25 AM
Coldsteel Re: Changing a pivot table... 09-16-2010, 04:15 AM
  1. #1
    Registered User
    Join Date
    06-21-2010
    Location
    Texas, San Antonio
    MS-Off Ver
    Excel 2007
    Posts
    57

    Changing a pivot table with a cell value

    Hello all,

    What I am trying to do is select a value in a named cell range called “DataSelection” to change the data field called” Name” within a Pivot Table called” PivotTable1”. I have been trying to get this code to work but I keep getting a run-time error ‘1004’ Method ‘Intersect’ of object ‘_Global’ failed. Whenever I try to select another data validation list I have on the worksheet, that has no relationship to PivotTable1.
    Here is the code:

    
    Option Explicit
    
    Const RegionRangeName As String = "DataSelection"
    Const PivotTableName As String = "PivotTable1"
    Const PivotFieldName As String = "Name"
    
    Public Sub UpdatePivotFieldFromRange(RangeName As String, FieldName As String, _
    PivotTableName As String)
    
        Dim rng As Range
        Set rng = Application.Range(RangeName)
        
        Dim pt As PivotTable
        Dim Sheet As Worksheet
        For Each Sheet In Application.ActiveWorkbook.Worksheets
            On Error Resume Next
            Set pt = Sheet.PivotTables(PivotTableName)
        Next
        If pt Is Nothing Then GoTo Ex
        
        On Error GoTo Ex
        
        pt.ManualUpdate = True
        Application.EnableEvents = False
        Application.ScreenUpdating = False
        
        Dim Field As PivotField
        Set Field = pt.PivotFields(FieldName)
        Field.ClearAllFilters
        Field.EnableItemSelection = False
        SelectPivotItem Field, rng.Text
        pt.RefreshTable
        
    Ex:
        pt.ManualUpdate = False
        Application.EnableEvents = True
        Application.ScreenUpdating = True
        
    End Sub
    
    Public Sub SelectPivotItem(Field As PivotField, ItemName As String)
        Dim Item As PivotItem
        For Each Item In Field.PivotItems
            Item.Visible = (Item.Caption = ItemName)
        Next
    End Sub
    
    Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    
        If Not Intersect(Target, Application.Range(RegionRangeName)) _
            Is Nothing Then
                UpdatePivotFieldFromRange _
                RegionRangeName, PivotFieldName, PivotTableName
                
      End If
    End Sub
    Thanks
    Last edited by Coldsteel; 09-16-2010 at 04:16 AM.

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