简体   繁体   English

在使用主要和次要y轴时如何使用vba将图表轴的比例从对数更改为线性

[英]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. 此数据同时使用主要和次要y轴,有时最好以线性标度或对数标度查看。 I have no trouble formatting axes scale to be logarithmic using the chart object's ".SetElement" method as so: 我可以很方便地使用图表对象的“ .SetElement”方法将轴刻度格式化为对数格式:

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

where "chartSheet" is a chart object 其中“ chartSheet”是图表对象

  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. 将使轴显示= none,不将显示单位设置为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. 我尝试过的另一种方法是使用轴对象的“ ScaleType”属性。 Per the documentation: 根据文档:

expression.Axes (Type, AxisGroup) expression.Axes(类型,AxisGroup)

Type can be xlValue, xlCategory, or xlSeriesAxis 类型可以是xlValue,xlCategory或xlSeriesAxis

AxisGroup can be xlPrimary or xlSecondary AxisGroup可以是xlPrimary或​​xlSecondary

ScaleType can be xlScaleLinear or xlScaleLogarithmic ScaleType可以是xlScaleLinear或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? 有谁知道上述方法可能有什么问题,还是知道另一种以编程方式将次要Y轴设置为具有线性比例并以正常的自然单位显示的方式?

----UPDATE------ ----更新------

I've discovered that the problem is more specific to just the x-axis or using "xlCategory". 我发现问题只针对x轴或使用“ xlCategory”。 I was able to get it to work and set a linear scale of the y1 and y2 axis using 我能够使其工作并使用以下命令设置y1和y2轴的线性比例

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. 关于枚举的注释:“ xlScaleLinear”和“ xlLinear”都代表“ -4132”,并且都可以与值轴一起使用,但不适用于类别。

----WORK AROUND SOLUTION for X-Axis------- ---- X轴周围的解决方案-------

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 我发现scaletype枚举是

xlLinear . xlLinear

Please try this. 请尝试这个。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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