change chart scale in vba

davers5

Active Member
Joined
Feb 20, 2002
Messages
255
I'm trying to change the value scale of a chart using VBA but I get the error "unable to set MinimumScale property of axis class". This is my first time manipulating a chart in VBA and I'm a little confused about the object classes. My charts are embedded in a worksheet and the code should scroll through each chart and change it's maximum value to the value of a cell on the worksheet. Everything works great until I get to actually assigning the values. Any advice? My code is below.

Thanks,

Dave

Code:
Private Sub ReScaleCharts()
    Dim c As Object
    Dim wksYTD As Worksheet
    Dim sngMaxScale As Single
    Dim sngMinScale As Single
        
    'assign worksheet to variable
    Set wksYTD = Worksheets("Rev_Exp")
    
    'loop through charts
    For Each c In wksYTD.ChartObjects
        Select Case c.Name
            Case "Rev_YTD"
                sngMaxScale = Range("revmaxscale")
                sngMinScale = 0
            Case "Exp_YTD"
                sngMaxScale = Range("expmaxscale")
                sngMinScale = 0
            Case "EBITDA_YTD"
                sngMaxScale = Range("ebitmaxscale")
                sngMinScale = 0
            Case Else
                MsgBox c.Parent.Name
        End Select
    
    'assign min and max scale values
        With c.Chart.Axes(xlValue)
            .MinimumScale = sngMinScale
            .MaximumScale = sngMaxScale
            .MinorUnitIsAuto = True
            .MajorUnitIsAuto = True
            .Crosses = xlAutomatic
            .ReversePlotOrder = False
            .ScaleType = xlLinear
            .DisplayUnit = xlNone
        End With
    Next c
End Sub
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What's the error message, and what line is it on? What's in the relevant cells?

A few comments:
Dim c As ChartObject
Dim your scale values as Double
Reference your range appropriately:
_ MyValue = wksYTD.Range("whatever").Value
Make sure you don't apply a 0 to a log scale.
Maybe you need sthg like:
_ MyMax = WorksheetFunction.Max(0, wksYTD.Range("whatever").Value)
_ MyMin = WorksheetFunction.Min(0, wksYTD.Range("whatever").Value)


- Jon
-------
Jon Peltier, Microsoft Excel MVP
http://www.geocities.com/jonpeltier/Excel/index.html
_______
 
Upvote 0

Forum statistics

Threads
1,225,483
Messages
6,185,264
Members
453,284
Latest member
osy25

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