0

Alright, here is my situation:

I have two pivot charts in a workbook, Chart A and Chart B (on separate sheets... they are big charts). Under each chart is a data validation drop-down menu with years listed in them (2010-2020). Selecting a year in the menu activates a macro which changes the information displayed for the given year. I'd like to write the macros so that the information in BOTH charts changes, even though an item from only one of the two menus is selected. Furthermore. I'd like to make it so that BOTH drop-down menus update. That is, I select '2010' from the drop-down for Chart A, BOTH Chart A and Chart B now display data for 2010, AND the menu under Chart B now displays 2010.

I got started and wrote the following macro:

Sub Chart2010()
'
' Chart2010 Macro
'

'
    Application.ScreenUpdating = False

    ReturnSheet = ActiveSheet.Name

    Sheets("VP MfgGroupPivot").Select
    ActiveSheet.PivotTables("MfgGroupPivot").ClearTable
    With ActiveSheet.PivotTables("MfgGroupPivot").PivotFields( _
        "VP: Manufacturer Group")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("MfgGroupPivot").PivotFields( _
        "E: Propulsion System Design")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("MfgGroupPivot").AddDataField ActiveSheet.PivotTables( _
        "MfgGroupPivot").PivotFields("VCR 2010"), "Sum of VCR 2010", xlSum

    Columns("B:H").Select
    Selection.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"

    Sheets("DCS MfgPivot").Select
    ActiveSheet.PivotTables("DCSMfgPivot").ClearTable
    With ActiveSheet.PivotTables("DCSMfgPivot").PivotFields( _
        "VP: Manufacturer Group")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("DCSMfgPivot").PivotFields( _
        "E: Propulsion System Design")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("DCSMfgPivot").AddDataField ActiveSheet.PivotTables( _
        "DCSMfgPivot").PivotFields("DCS Rev 2010"), "Sum of DCS Rev 2010", xlSum

    Columns("B:H").Select
    Selection.NumberFormat = "_($* #,##0_);_($* (#,##0);_($* ""-""??_);_(@_)"

    If ReturnSheet = "VP MfgGroupChart" Then
        Sheets("DCS MfgChart").Select
        [C37].Value = 2010
    ElseIf ReturnSheet = "DCS MfgChart" Then
        Sheets("VP MfgGroupChart").Select
        [C37].Value = 2010
    End If

    Sheets(ReturnSheet).Select

End Sub

This seems like it would work... and it should, were it not for the routine that calls the macros based on the drop-down changing (below)

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    Select Case Range("C37")

        Case "2010"
            Call Chart2010
        Case "2011"
            Call Chart2011
        Case "2012" 
            Call Chart2012
        Case "2013"
            Call Chart2013
        Case "2014"
            Call Chart2014
        Case "2015"
            Call Chart2015
        Case "2016"
            Call Chart2016
        Case "2017"
            Call Chart2017
        Case "2018"
            Call Chart2018
        Case "2019"
            Call Chart2019
        Case "2020"
            Call Chart2020

    End Select

End Sub

So my if statement at the end of the Chart2010 routine causes an infinite loop, since calling the macros is based on a sheet change.

So the question is (and sorry for being so long winded), is there a way to make both drop-down menus update without calling the macro over and over again?

Thanks.

-Sean

4

1 回答 1

2

尝试在事件过程中关闭(禁用)events一段时间:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    'add this at the beginning
    Application.EnableEvents = false

   'your Select case code here

   'add this at the end
    Application.EnableEvents = true

End Sub
于 2013-07-01T13:06:26.843 回答