使用宏/ VBA将主轴和辅助轴与Y = 0对齐

时间:2018-07-30 09:48:21

标签: excel vba excel-vba

我正在尝试使Excel宏在具有主轴和副轴的图表上工作。目的是将两个轴都对准Y=0。辅助轴可能具有负值。

链接:https://peltiertech.com/Excel/Charts/AlignXon2Ys.html

我找到了Peltier的代码,但是一直遇到错误:

  

“对象不支持此属性或方法”

通过调试将我定向到With .Axes(2, 1)

任何帮助将不胜感激!

' Helper Macros
Sub AlignY_PrimaryMinimum()
  AlignY 1
End Sub

Sub AlignY_PrimaryMaximum()
  AlignY 2
End Sub

Sub AlignY_SecondaryMinimum()
  AlignY 3
End Sub

Sub AlignY_SecondaryMaximum()
  AlignY 4
End Sub

' The Main Event
Sub AlignY(FreeParam As Integer)
  '' FreeParam: AXIS ALLOWED TO VARY
  '' 1: Y1 (PRI) MIN
  '' 2: Y1 (PRI) MAX
  '' 3: Y2 (SEC) MIN
  '' 4: Y2 (SEC) MAX
  Dim Y1min As Double
  Dim Y1max As Double
  Dim Y2min As Double
  Dim Y2max As Double

  With ActiveSheet.ChartObjects("Chart 1")
    With .Axes(2, 1)
      Y1min = .MinimumScale
      Y1max = .MaximumScale
      .MinimumScaleIsAuto = False
      .MaximumScaleIsAuto = False
    End With
    With .Axes(2, 2)
      Y2min = .MinimumScale
      Y2max = .MaximumScale
      .MinimumScaleIsAuto = False
      .MaximumScaleIsAuto = False
    End With
    Select Case FreeParam
      Case 1
        If Y2max <> 0 Then _
          .Axes(2, 1).MinimumScale = Y2min * Y1max / Y2max
      Case 2
        If Y2min <> 0 Then _
          .Axes(2, 1).MaximumScale = Y1min * Y2max / Y2min
      Case 3
        If Y1max <> 0 Then _
          .Axes(2, 2).MinimumScale = Y1min * Y2max / Y1max
      Case 4
        If Y1min <> 0 Then _
          .Axes(2, 2).MaximumScale = Y2min * Y1max / Y1min
    End Select
  End With
End Sub

0 个答案:

没有答案