Setting (X) chart scale minimum and maximum

User avatar
ErikJan
BronzeLounger
Posts: 1253
Joined: 03 Feb 2010, 19:59
Location: Terneuzen, the Netherlands

Setting (X) chart scale minimum and maximum

Post by ErikJan »

Setting the minimum and maximum scale in a chart using VBA is easy. Lets' suppose the current minimum = 10 and the maximum = 20 and we want to set the new min and max to be resp 30 and 40... We would do this, right?

Code: Select all

With Charts("Name").Axes(xlCategory)
.MinimumScale = 30
.MaximumScale = 40
End With
However, the moment we set the new minimum, that value is now larger than the old maximum (=20)... wouldn't that trigger an error then??? If that would be the case, could I fix like below?

Code: Select all

With Charts("Name").Axes(xlCategory)
.MinimumScaleIsAuto = True
.MaximumScaleIsAuto = True
.MinimumScale = 30
.MaximumScale = 40
End With

User avatar
HansV
Administrator
Posts: 78524
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Setting (X) chart scale minimum and maximum

Post by HansV »

There is no need for that. When you set the minimum scale to higher than the current maximum scale, Excel doesn't protest but silently sets the maximum scale to Automatic. The next statement will set the maximum scale to the desired value.

However, if you used

.MinimumScale = 30
.MaximumScale = 25

the latter statement would undo the result of the first: the minimum scale would be set to Automatic.
Best wishes,
Hans