1

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

“填充策略”和“报告”选项卡模块中的代码:

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
4

2 回答 2

0

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

于 2013-08-27T16:17:30.270 回答
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
于 2013-08-27T16:26:06.533 回答