使用Worksheet_Change时调用的子句将无法运行

时间:2015-09-29 18:59:48

标签: excel-vba call worksheet vba excel

我正在尝试调用一个名为ScaleAxes的子,它位于其他地方的模块中。 ScaleAxes代码是:

Option Explicit
Sub ScaleAxes()
  Worksheets("Dashboard").ChartObjects("Measure Chart").Activate
  With ActiveChart.Axes(xlCategory, xlPrimary)
    .MaximumScale = Sheets("Lists and Data").Range("R7").Value
    .MinimumScale = Sheets("Lists and Data").Range("Q7").Value
    .MajorUnit = Sheets("Lists and Data").Range("S7").Value
  End With
  With ActiveChart.Axes(xlValue, xlPrimary)
    .MaximumScale = Sheets("Lists and Data").Range("R9").Value
    .MinimumScale = Sheets("Lists and Data").Range("Q9").Value
    .MajorUnit = Sheets("Lists and Data").Range("S9").Value
  End With
End Sub

我试图调用它的代码在这里:

Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Range("H2:N2")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("PI DataLink")
            Set automationObject = addIn.Object
        Dim MyRange As Range
            Set MyRange = Range("J2")
            MyRange.Select
            automationObject.SelectRange
            automationObject.ResizeRange
            Application.CalculateFull
     End If
     If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
        Call ScaleAxes
    End If
End Sub

当我单独运行时,ScaleAxes会自行运行,并且在运行它的代码时不会出现错误。问题是上面的代码似乎运行正常,但ScaleAxes代码不运行。我也尝试从ScaleAxes()子代码中提取代码并将其粘贴到代码的工作部分所在的IF函数中,但这也不起作用。

您可以提供任何帮助我解决此问题的帮助。

编辑1:

这是我当前状态的代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim KeyCells As Range
Set KeyCells = Sheets("Dashboard").Range("C2:G2")
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
        Dim addIn As COMAddIn
        Dim automationObject As Object
        Set addIn = Application.COMAddIns("PI DataLink")
            Set automationObject = addIn.Object
        Dim MyRange As Range
            Set MyRange = Range("N2")
            MyRange.Select
            automationObject.SelectRange
            automationObject.ResizeRange
            Application.CalculateFull
    End If
    If Not Application.Intersect(KeyCells, Range(Target.Address)) _
        Is Nothing Then
        Call ScaleAxes
    End If
End Sub

同样的问题正在发生。上面的代码在Sheet2(仪表板)中,被调用的代码在Module2中。

编辑2:

就像更新一样。当我使用命令按钮调用ScaleAxes子时它可以工作。这可以作为临时解决方案,但是如果可能的话,我希望与其余代码同时调用sub。

0 个答案:

没有答案