Chart X axis scaling

JT001

New Member
Joined
Nov 18, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

I've been successfully using some simple VBA code for years in Excel 2013 to adjust the X axis scale of a chart. I've just upgraded to Excel 365 and the code no longer works - I get Run-Time Error 91, Object variable or With block variable not set.

This is the code...

XLScale.png


Please can someone tell me what to do to fix it?

Thanks.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi JT001. Trial changing the active sheet to a specific sheet (ie. Sheets("Sheet1").Range("c5").Value). The "activechart" might also might need to be changed to a specific chart (ie. Sheets("Sheet1").ChartObjects("Chart 1").Chart) HTH. Dave
ps. Welcome to the Board!
 
Upvote 0
Works fine for me under 365. The only time I get runtime error 91 is if there is no ActiveChart object, which I suspect is your problem. You can test for this with a slight code modification

VBA Code:
Sub ScaleXAxisWholeCycle()
    Dim Ch As Chart
    
    On Error Resume Next
    Set Ch = ActiveChart
    On Error GoTo 0
    
    If Not Ch Is Nothing Then
        With ActiveChart.Axes(xlCategory, xlPrimary)
            .MinimumScale = ActiveSheet.Range("C5").Value
            .MaximumScale = ActiveSheet.Range("C6").Value
        End With
    Else
        MsgBox "No Chart Found"
    End If
End Sub

On another subject, welcome to the forum. When you post code, please post the actual text (and use code tags) as I have done above. Images of code are hard to work with.
 
Upvote 0
Thank for the suggestions.

I've updated the code as per rlv01 suggestion and now, regardless of whether I've selected the chart or not, I get the 'No Chart Found' message so it looks like Excel isn't seeing the chart as Active even though it's been selected.

Any ideas? Could it be because the workbook was originally created in Excel 1013 (or maybe even older!)?

Many thanks,

Jon
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top