工作表激活无限循环

时间:2013-08-27 16:09:52

标签: excel excel-vba vba

VBA新手在这里。我搜索了回复,但找不到任何有用的东西。我在Excel标签中有一个图表。我希望图表的Y轴根据我传入的参数动态更新,所以我写了一个简单的宏来在VBA模块中这样做(我在图表中的某个日期之后有一个带零的系列,所以我可以'只需让Excel选择一个自动轴)。当我手动调用它时(例如,通过按钮),宏工作正常,但我希望每次用户选择“填充策略”或“报告”选项卡时调用它。我正在使用Worksheet_Activate代码(见下文),但由于“表格(”填充策略“),我陷入无限循环。选择”和“表格(”报告“)。选择”更新“的”药水“ “宏(基本上反复调用我的Worksheet_Activate代码)。我该如何解决这个问题?有没有办法重新设计“UpdateChartAxes”代码,以便它不需要select语句?任何帮助将不胜感激。

“填充策略”和“报告”标签模块中的代码:

Private Sub Worksheet_Activate()
Call UpdateChartAxes
End Sub

开放模块中的代码:

Public Sub UpdateChartAxes()
Application.ScreenUpdating = False
Call ShowSheets

'WorkforceReportChart
    Dim WorkforceReportChartMin As Long
    Dim WorkforceReportChartMax As Long
    Sheets("Chart Data").Select
    WorkforceReportChartMin = WorksheetFunction.min(Range("ReportGraphDataRangeExcludingHistoricSeries")) * 0.95
    WorkforceReportChartMax = WorksheetFunction.Max(Range("ReportGraphDataRangeExcludingHistoricSeries")) * 1.05
    Sheets("Report").Select
    With ActiveSheet.ChartObjects("WorkforceReportChart").Chart
        With .Axes(xlValue)
            .MinimumScale = WorkforceReportChartMin
            .MaximumScale = WorkforceReportChartMax
        End With
    End With

'FillStrategyChart
    Dim FillStrategyChartMin As Long
    Dim FillStrategyChartMax As Long
    Sheets("Chart Data").Select
    FillStrategyChartMin = WorksheetFunction.min(Range("FillStrategyGraphDataRangeExcludingHistoricSeries")) * 0.95
    FillStrategyChartMax = WorksheetFunction.Max(Range("FillStrategyGraphDataRangeExcludingHistoricSeries")) * 1.05
    Sheets("Fill Strategy").Select
    With ActiveSheet.ChartObjects("FillStrategyChart").Chart
        With .Axes(xlValue)
            .MinimumScale = FillStrategyChartMin
            .MaximumScale = FillStrategyChartMax
        End With
    End With

Call HideSheets
End Sub

2 个答案:

答案 0 :(得分:0)

您可以避免编写宏来执行此操作 - 只需将图表基于动态命名范围。有关详细信息,请参阅此链接:http://www.ozgrid.com/Excel/DynamicRanges.htm

答案 1 :(得分:0)

试试这样:

Public Sub UpdateChartAxes()
Application.ScreenUpdating = False
Call ShowSheets
Dim curWs as Worksheet

'WorkforceReportChart
    Dim WorkforceReportChartMin As Long
    Dim WorkforceReportChartMax As Long
    'Sheets("Chart Data").Select
    Set curWs = Sheets("Chart Data")
    WorkforceReportChartMin = WorksheetFunction.min(Range(curWs.Name & "!ReportGraphDataRangeExcludingHistoricSeries")) * 0.95
    WorkforceReportChartMax = WorksheetFunction.Max(Range(curWs.Name & "!ReportGraphDataRangeExcludingHistoricSeries")) * 1.05
    'Sheets("Report").Select
    Set curWs = Sheets("Report")
    With curWs.ChartObjects("WorkforceReportChart").Chart
        With .Axes(xlValue)
            .MinimumScale = WorkforceReportChartMin
            .MaximumScale = WorkforceReportChartMax
        End With
    End With

'FillStrategyChart
    Dim FillStrategyChartMin As Long
    Dim FillStrategyChartMax As Long
    'Sheets("Chart Data").Select
    Set curWs = Sheets("Chart Data")
    FillStrategyChartMin = WorksheetFunction.min(Range(curWs.Name & "!FillStrategyGraphDataRangeExcludingHistoricSeries")) * 0.95
    FillStrategyChartMax = WorksheetFunction.Max(Range(curWs.Name & "!FillStrategyGraphDataRangeExcludingHistoricSeries")) * 1.05
    'Sheets("Fill Strategy").Select
    Set Sheets("Fill Strategy")
    With curWs.ChartObjects("FillStrategyChart").Chart
        With .Axes(xlValue)
            .MinimumScale = FillStrategyChartMin
            .MaximumScale = FillStrategyChartMax
        End With
    End With

Call HideSheets
End Sub