文件打开时自动更新graphics缩放失误的dynamicmacros

我有一个对我来说毫无意义的问题。

Option Explicit Private Sub Worksheet_Calculate() Dim Chtob As ChartObject Dim wks As Worksheet Set wks = ActiveSheet On Error GoTo Terminate For Each Chtob In ActiveSheet.ChartObjects With Chtob.Chart If wks.Range("$G$2").Value <> .Axes(xlCategory).MaximumScale Then .Axes(xlCategory).MaximumScale = wks.Range("$G$2").Value End If If wks.Range("$C$2").Value <> .Axes(xlCategory).MinimumScale Then .Axes(xlCategory).MinimumScale = wks.Range("$C$2").Value End If If wks.Range("$G$2").Value <> .Axes(xlCategory, xlSecondary).MaximumScale Then .Axes(xlCategory, xlSecondary).MaximumScale = wks.Range("$G$2").Value End If If wks.Range("$C$2").Value <> .Axes(xlCategory, xlSecondary).MinimumScale Then .Axes(xlCategory, xlSecondary).MinimumScale = wks.Range("$C$2").Value End If End With Next Exit Sub Terminate: MsgBox "Storm Event Not Valid, Please check if such event number exists" End Exit Sub 

此macros用于具有两个图表的选项卡上。 当某个单元格更改时,macros将更新graphics比例。 这个选项卡会被重复多次以显示不同的时间事件。
当别人试图打开这个文件时就会出现这个问题。 文件打开的那一刻,就会popup与创build标签数量相同的错误。 这由于某种原因导致不同的选项卡与不同的graphics重置它的X尺度。 这个不同的选项卡没有附加dynamicmacros,也没有使用其他的macros。

我想说的是,不同版本的Excel可能是问题的一部分,但有时候这种情况不会发生。

它应该工作的方式是当有人在单元格B2中input错误的值时,macros不能执行。 所以不要进入debugging,而是得到一个错误信息。 所以我需要macros的错误部分在那里。

我应该提到该选项卡还具有另一个dynamicmacros,如果同一个单元格更改,则会自动重命名该选项卡名称。

 Sub Worksheet_Change(ByVal Target As Range) If Target.Address = "$B$2" Then If Not Target.HasFormula Then If Not Target.Value = vbNullString Then On Error GoTo ErrHandler ActiveSheet.Name = "Event" & " " & Target.Value End If End If End If Exit Sub ErrHandler: MsgBox "Error " & Err & ":" & Error(Err) On Error GoTo 0 End Sub 

感谢Scott提出的意见,我的问题从来没有再强化我刚刚改变了Set wks = Activesheet Set wks = Me ,然后在脚本中更改所有的wksMe

 Option Explicit Private Sub Worksheet_Calculate() Dim Chtob As ChartObject Dim wks As Worksheet Set wks = Me On Error GoTo Terminate For Each Chtob In Me.ChartObjects With Chtob.Chart If wks.Range("$G$2").Value <> .Axes(xlCategory).MaximumScale Then .Axes(xlCategory).MaximumScale = wks.Range("$G$2").Value End If If wks.Range("$C$2").Value <> .Axes(xlCategory).MinimumScale Then .Axes(xlCategory).MinimumScale = wks.Range("$C$2").Value End If If wks.Range("$G$2").Value <> .Axes(xlCategory, xlSecondary).MaximumScale Then .Axes(xlCategory, xlSecondary).MaximumScale = wks.Range("$G$2").Value End If If wks.Range("$C$2").Value <> .Axes(xlCategory, xlSecondary).MinimumScale Then .Axes(xlCategory, xlSecondary).MinimumScale = wks.Range("$C$2").Value End If End With Next Exit Sub Terminate: MsgBox "Storm Event Not Valid, Please check if such event number exists" End End Sub