Dynamic Macro That Auto Updates Graph Scale Misfiring When File Opens

时间:2016-02-12 20:59:16

标签: excel vba excel-vba

I have an issue that makes no sense to me.

$docx->addBreak(array('type' => 'page'));
            $options = array(
            'src' => $value['110333870356bc784cf1b6b0.56722572.png'],
            'imageAlign' => 'center',
            'scaling' => 100,
            'spacingTop' => 10,
            'spacingBottom' => 0,
            'spacingLeft' => 0,
            'spacingRight' => 20,
            'textWrap' => 0,

            );
            $docx->addImage($options); 

This macro is used on a tab that has two charts. When a certain cell changes the macro updates the graph scale. This tab will be then duplicated numerous times to show different time events.
The issue arises when someone else tries to open this file. The moment the file is open they get the error to pop up as many times as the amount of tabs created. This for some reason causes a different tab with a different graph to reset it's x scale. This different tab does not have the dynamic macro attached to it and no other macros are being used.

I want to say that a different version of Excel might be part of the problem, but there are times when this doesn't happen.

The way it should work is when somebody enters the wrong value in cell B2 the macro can't execute. So instead of going into debug, one gets an error message. So I need the error portion of the macro to be there.

I should mention that the tab also has another dynamic macro that automatically renames the tab name if the same cell changes.

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

1 个答案:

答案 0 :(得分:0)

感谢斯科特的评论,我的问题再也没有出现过 我刚刚将Set wks = Activesheet更改为Set wks = Me,然后将脚本中的所有wks更改为Me

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