+ Reply to Thread
Results 1 to 10 of 10

Combo Box Code

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-11-2008
    Posts
    156

    Combo Box Code

    Hi there,

    The following code allows me to contol a pivot table's page fields using a combobox when both the pivot table and the combo box are on the same page.

    Please can you show me how to adapt the code so that I can have the combo box in a separate sheet to the pivot table.

    Many thanks

    Behind Pivot Sheet:

    Option Explicit  
      
    Private Sub ComboBox1_Change()  
      
    If Me.ComboBox1.ListIndex < 0 Then  
      
    Exit Sub  
      
    End If  
      
    Select Case LCase(Me.ComboBox1.Value)  
      
    Case Is = "scenario 1"  
      
    With Me.PivotTables("pivottable1")  
      
    .PageFields("Name1").CurrentPage = "asdf1"  
      
    .PageFields("name2").CurrentPage = "qwer1"  
      
    End With  
      
    Case Is = "scenario 2"  
      
    With Me.PivotTables("pivottable1")  
      
    .PageFields("Name1").CurrentPage = "asdf2"  
      
    .PageFields("name2").CurrentPage = "qwer3"  
      
    End With  
      
    End Select  
      
    End Sub

    Behind ThisWorkbook module:

    Option Explicit  
      
    Private Sub Workbook_Open()  
      
    With Worksheets("Sheet2").ComboBox1  
      
    .Clear  
      
    .AddItem "Scenario 1"  
      
    .AddItem "Scenario 2"  
      
    End With  
      
    End Sub

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    The ME reference in your code refers to the worksheet the pivottable and combo box are on.

    If you replace pivot tables ME reference with a reference to the worksheet you should be okay.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    04-11-2008
    Posts
    156
    Andy,

    Thank you for your response.

    I have tried the referencing as you described but it does not seem to work (although I am not getting an error message).

    Would you mind taking a look at the attached to see where I am going wrong.

    Many thanks
    Attached Files Attached Files

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Try

    Private Sub Workbook_Open()
      
        With Worksheets("Sheet3").Shapes("ComboBox1")
            With .OLEFormat.Object.Object
                .Clear
                .AddItem "<20"
                .AddItem "20-29"
            End With
        End With
      
    End Sub

  5. #5
    Forum Contributor
    Join Date
    04-11-2008
    Posts
    156
    Andy,

    Thanks again. Unfortunately still no joy. Please could you take another look?

    Thanks so much.

    My code is as follows (spreadsheet also attached):

    Sheet 4 (Sheet PT is on):
    Option Explicit
      
    Private Sub ComboBox1_Change()
      
    If Worksheets("Sheet3").ComboBox1.ListIndex < 0 Then
      
    Exit Sub
      
    End If
      
    Select Case LCase(Worksheets("Sheet3").ComboBox1.Value)
      
    Case Is = "<20"
      
    With Worksheets("Sheet4").PivotTables("pivottable1")
      
    .PageFields("Age").CurrentPage = "<20"
      
    End With
      
    Case Is = "20-29"
      
    With Worksheets("Sheet4").PivotTables("pivottable1")
      
    .PageFields("Age").CurrentPage = "20-29"
      
      
    End With
      
    End Select
      
    End Sub
    This Workbook:
    Option Explicit
      
    Private Sub Workbook_Open()
      
        With Worksheets("Sheet3").Shapes("ComboBox1")
            With .OLEFormat.Object.Object
                .Clear
                .AddItem "<20"
                .AddItem "20-29"
            End With
        End With
      
    End Sub
    Thank you
    Attached Files Attached Files
    Last edited by penfold; 07-15-2008 at 07:13 AM.

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Private Sub ComboBox1_Change()
    
    If Me.ComboBox1.ListIndex < 0 Then
        Exit Sub
    End If
      
    Select Case LCase(Me.ComboBox1.Value)
    Case Is = "<20"
        With Worksheets("Sheet4").PivotTables("pivottable1")
          
        .PageFields("Age").CurrentPage = "<20"
          
        End With
    Case Is = "20-29"
        With Worksheets("Sheet4").PivotTables("pivottable1")
            .PageFields("Age").CurrentPage = "20-29"
        End With
    End Select
      
    End Sub
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    04-11-2008
    Posts
    156
    Hi Andy,

    Thanks again.

    I Copy and Pasted your code as per the attached but still no joy. I don't get an error message it just doesn't seem to work.

    I'm not sure if you revised the attachment because I can't seem to open it without it corrupting.

    Please can I ask for your help once more.

    Many thanks
    Attached Files Attached Files

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    Yes but you posted the combo boxes change event code to a sheet which does not have a combobox.

    The event code needs to be on the same sheets as the combobox, as per my example.

  9. #9
    Forum Contributor
    Join Date
    04-11-2008
    Posts
    156
    Thank you!! I've managed to download the attachment and it works a treat!!

    I've tried to add a 2nd combo box to control the 2nd page field (Gender) by repeating and amending the code but I've not managed to succeed.

    Please can you take a look at the attached?

    Thank you.
    Attached Files Attached Files

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482
    You were using LCase in the select statement which made the case either "m" or "f", which of course it never would be.
    Try this revision.

    Private Sub ComboBox2_Change()
    
    If Me.ComboBox2.ListIndex < 0 Then
        Exit Sub
    End If
      
    Select Case UCase(Me.ComboBox2.Value)
    Case Is = "M"
        With Worksheets("Sheet4").PivotTables("pivottable1")
          
        .PageFields("Gender").CurrentPage = "M"
          
        End With
    Case Is = "F"
        With Worksheets("Sheet4").PivotTables("pivottable1")
            .PageFields("Gender").CurrentPage = "F"
        End With
    End Select
    
    End Sub

+ 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