简体   繁体   中英

How to use vba to change chart axes scale to linear from log when using primary and secondary y axes

I have several macros to batch process data and build charts. This data uses both the primary and secondary y-axes and sometimes it's better viewed on a linear scale or log scale. I have no trouble formatting axes scale to be logarithmic using the chart object's ".SetElement" method as so:

chartSheet.SetElement (msoElementPrimaryCategoryAxisLogScale)
chartSheet.SetElement (msoElementPrimaryValueAxisLogScale)
chartSheet.SetElement (msoElementSecondaryValueAxisLogScale)

where "chartSheet" is a chart object

  Dim chartSheet As Chart
  Set chartSheet = Sheets(chartExistsIdx)

That works, but I can't find an enum or value that uses that method to set it back to a linear scale. For each axis there are options to set the scale to be linear with units using

msoElementPrimaryCategoryAxisThousands
msoElementPrimaryCategoryAxisMillions
msoElementPrimaryCategoryAxisBillions

however, this changes the axis display units to also be in thousands, millions, or billions.

msoElementPrimaryCategoryAxisNone

will make the axis display = none, not set the display units to none.

I have not found a way to use this method to either directly set the scale to linear or set it using the units, but then set the set the units to "none".


Another approach I've tried is to use the axis objects "ScaleType" property. Per the documentation:

expression.Axes (Type, AxisGroup)

Type can be xlValue, xlCategory, or xlSeriesAxis

AxisGroup can be xlPrimary or xlSecondary

ScaleType can be xlScaleLinear or xlScaleLogarithmic

so I've tried

chartSheet.Axes(xlCategory, xlSecondary).ScaleType = xlScaleLinear

but this will give me a method failed error.

Does anyone know what might be wrong with the approaches above or know of another way to programmatically set the secondary Y-axis to have a linear scale and display in normal, native units?

----UPDATE------

I've discovered that the problem is more specific to just the x-axis or using "xlCategory". I was able to get it to work and set a linear scale of the y1 and y2 axis using

this works

chartSheet.Axes(xlValue, xlPrimary).ScaleType = xlScaleLinear
chartSheet.Axes(xlValue, xlSecondary).ScaleType = xlScaleLinear

however this does NOT work

chartSheet.Axes(xlCategory, xlPrimary).ScaleType = xlScaleLinear

A note on the enums: "xlScaleLinear" and "xlLinear" both represent "-4132" and both will work with the Value axes, but not the category.

----WORK AROUND SOLUTION for X-Axis-------

I worked out the syntax to set the display units, so can now use this work around. Set the scale to linear with "thousands" as the display unit, then reset the display unit property of the axis to none.

chartSheet.SetElement (msoElementPrimaryCategoryAxisThousands)
chartSheet.Axes(xlCategory, xlPrimary).DisplayUnit = xlNone

i tried recording while applying log scale and unsetting it. i found that scaletype enum was

xlLinear .

Please try this. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM