Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA Run-time error 91

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
like image 376
kat2000 Avatar asked Jun 17 '26 22:06

kat2000


1 Answers

Add the following line before the with statement.

ActiveSheet.ChartObjects("Chart 1").Activate

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

like image 179
David912 Avatar answered Jun 19 '26 12:06

David912



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!