+ Reply to Thread
Results 1 to 3 of 3

Combo Box Change Event with Macro Call

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2011
    Location
    Oakland, CA USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Combo Box Change Event with Macro Call

    First post ever.

    I have a macro (Excel 2003) which refreshes a number of pivot tables based on values in three different cells on a separate worksheet. The macro runs fine when fired independently.

    I added a dropdown list to select values for each of these cells and triggered the firing of my macro using a worksheet change event. This works perfectly.

    What I really want is to use three combo boxes to select the values and to trigger the macro using a combobox change event. When I try this, however the macro bombs on the third line of code. I'm pretty new to VBA and I've never tried to use the combobox change event so I may be missing something really basic.

    Here's the code I'm using for the change event. (The macro name is "RefreshPivots"). I've placed the following code on the worksheet object (within the VBA editor) for the worksheet where the combo box was placed. Again the macro runs perfectly when I run it on its own which leads me to suspect that there's something wrong with my combox change event code.

    Sub Combobox1_Change()
                 RefreshPivots
                End Sub
    Here's a snippet of the macro code which refreshes the first of several pivot tables:

    'Refresh pivots
    '  This block refreshes the pivot cache of all pivot tables to deal
    '   with problems where drop-down list for Page field "SPECIALTY" was getting
    '   corrupted dropping items from list and adding an item "0" in the pivot table
    '   Page field selection list. Code rebuilds the
    '   pivots by
    '       1) Removing items from the "Page" dimension of each pivot table
    '       2) Refreshing the pivot cache
    '       3) Adding the Page fields back
    '       4) Supplying the user selected values for each of the three page fields
    '
    '    **********************************************************************
    '    **********************************************************
    '    ****                                                              ****
    '    ****   1   Refresh Pivot Table:                          ****
    '    ****       Filled_2_yrs_ago_Counts_by_Month   ****
    '    ****       (Worksheet: Filled Reqs - Counts       ****
    '    ****                                                              ****
    '    **********************************************************
    '    **********************************************************
    
    '               ***************************************************
    '   *************  (a) Refresh Cache
    '               ***************************************************
        Sheets("Filled Reqs - Counts").Visible = True
        Sheets("Filled Reqs - Counts").Select
        
        ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("SPECIALTY"). _
            Orientation = xlHidden
        ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("MED_CTR"). _
            Orientation = xlHidden
        ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("RECRUITER"). _
            Orientation = xlHidden
    
        ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotCache.Refresh ''
    
        With ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("SPECIALTY")
            .Orientation = xlPageField
            .Position = 1
        End With
    
        With ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("MED_CTR")
            .Orientation = xlPageField
            .Position = 1
        End With
    
        With ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("RECRUITER")
            .Orientation = xlPageField
            .Position = 1
        End With
    
    '               ***************************************************
    '   *************  (b) Refresh Pivot with user-selected values
    '               ***************************************************
       ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("SPECIALTY"). _
            CurrentPage = Range("A1").Value
    
       ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("MED_CTR"). _
            CurrentPage = Range("A2").Value
    
       ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("RECRUITER"). _
            CurrentPage = Range("A3").Value


    The line of code (executable line3) where the macro fails is:
        
        ActiveSheet.PivotTables("Filled_2_yrs_ago_Counts_by_Month").PivotFields("SPECIALTY"). _
            Orientation = xlHidden
    The error is:

    Run-time error '1004'
    Unable to get the PivotFields property of the PivotTable class

    Thanks for any help you can offer
    Last edited by Bosco6; 07-21-2011 at 02:46 PM.

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Combo Box Change Event with Macro Call

    Hi Bosco6 and welcome to the forum. Please take a moment to read the forum rules located here and wrap your code in code tags as per Rule #3. Once you do that, someone will be able to help you.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    07-20-2011
    Location
    Oakland, CA USA
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Combo Box Change Event with Macro Call

    Thanks for that. Proper convention for pasting code into my posts is duly noted.

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