3

I assigned a macro to a shape, but I'm receiving a Run-time error '91' Object variable or With block variable not set. But, when I run the Macro from the Developer tab, it works. The "ActiveChart.Axes" line is where the error is coming from. I'm new to VBA so any help would be appreciated. Should I replace "ActiveChart.Axes" with "ActiveSheet.ChartObjects"?

Option Explicit

Sub ScaleAxes()
 With ActiveChart.Axes(xlValue, xlPrimary)
    .MaximumScale = ActiveSheet.Range("B14").Value
    .MinimumScale = ActiveSheet.Range("B15").Value
    .MajorUnit = ActiveSheet.Range("B16").Value
  End With
End Sub
Community
  • 1
  • 1
kat2000
  • 31
  • 1

1 Answers1

4

Add the following line before the with statement.

ActiveSheet.ChartObjects("Chart 1").Activate

Assuming "chart 1" is the name of your chart.

David912
  • 378
  • 1
  • 2
  • 11
  • I tested and this works. Up voted as this is an odd problem and the solution wasn't obvious. – Yogi Feb 26 '16 at 22:18