EXCEL 2016 VBA:交换主要和次要Y轴

时间:2017-10-29 22:04:32

标签: excel vba graph

我试图以编程方式生成一个叠加了正常曲线的直方图。我从各种帮助页面和帖子中收集的一般策略是创建xlXYScatterSmoothNoMarkers,为正常曲线的x和y值添加数据系列,然后将我的直方图数据(中断和频率)添加为另一个数据系列,并且然后将直方图数据系列更改为xlColumnClustered,并进行一些最终格式化。但是,当我接近结束时,正常曲线的密度值在主Y轴上,频率值在次Y轴上。我正在寻找一种方法来切换它们。以下是目前图表的图片:

Screen shot of Graph So Far

以下是我的代码:

===

Set MyChart = ActiveSheet.ChartObjects.Add(Left:=Cells(Row0 + (j3 - 1) * 22, Col0 + ChartStart + 1 + (k3 - 1) * 7).Left, _
                    Top:=Cells(Row0 + (j3 - 1) * 22, Col0 + ChartStart + 1 + (k3 - 1) * 7).Top, _
                    Width:=350, _
                    Height:=300).Chart

                MyChart.ChartType = xlXYScatterSmoothNoMarkers
                MyChart.HasLegend = False


        With MyChart
                    'XAxis Label
                    .Axes(xlCategory, xlPrimary).HasTitle = True
                    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Model Residuals"
                    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Font.Bold = True
                    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Font.Name = "TimesNewRoman"
                    .Axes(xlCategory, xlPrimary).AxisTitle.Characters.Font.Size = 10
                    .Axes(xlCategory).TickLabelPosition = xlTickLabelPositionLow
                    'YAxis Label
                    .Axes(xlValue, xlPrimary).HasTitle = True
                    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Frequency"
                    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Font.Bold = True
                    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Font.Name = "TimesNewRoman"
                    .Axes(xlValue, xlPrimary).AxisTitle.Characters.Font.Size = 10
                    'Format Bars
                    '.ChartGroups(1).GapWidth = 0
                    '.ChartGroups(1).Overlap = 100
                End With

                'Remove Gridlines
                For Each axs In MyChart.Axes
                    axs.HasMajorGridlines = False
                    axs.HasMinorGridlines = False
                Next

                With MyChart.SeriesCollection.NewSeries
                    .Values = NormCurve
                    .XValues = XGraph
                    'Format
                End With

                'New X Axis Limits
                'MyChart.Axes(xlCategory, xlPrimary).MinimumScale = WorksheetFunction.Floor(XGraph(1), 100)
                'MyChart.Axes(xlCategory, xlPrimary).MaximumScale = WorksheetFunction.Ceiling(XGraph((ZMax - ZMin) * 100 + 1), 100)


                ReDim XLim(1 To 2)
                ReDim YLim(1 To 2)
                'MyChart.Axes(xlCategory, xlPrimary).MinimumScale = XGraph(1)
                'MyChart.Axes(xlCategory, xlPrimary).MaximumScale = XGraph((ZMax - ZMin) * 100 + 1)
                XLim(1) = MyChart.Axes(xlCategory, xlPrimary).MinimumScale
                XLim(2) = MyChart.Axes(xlCategory, xlPrimary).MaximumScale
                YLim(1) = MyChart.Axes(xlValue, xlPrimary).MinimumScale
                YLim(2) = MyChart.Axes(xlValue, xlPrimary).MaximumScale


                With MyChart
                    .SeriesCollection.NewSeries
                    .FullSeriesCollection(2).Name = "=""Histogram"""
                    .FullSeriesCollection(2).XValues = "='Sheet1 (2)'!$B$10:$B$16"
                    .FullSeriesCollection(2).Values = "='Sheet1 (2)'!$C$10:$C$16"
                    .FullSeriesCollection(2).AxisGroup = 2
                    .FullSeriesCollection(2).ChartType = xlColumnClustered
                    .ChartGroups(1).GapWidth = 0
                    .FullSeriesCollection(2).Format.Fill.Visible = msoFalse
                End With

===

我在网上找到了这个代码并试了一下:

===

              Dim oSeries As Series

                For Each oSeries In MyChart.SeriesCollection
                    If oSeries.AxisGroup = xlPrimary Then
                        oSeries.AxisGroup = xlSecondary
                    Else
                        oSeries.AxisGroup = xlPrimary
                    End If
                Next oSeries`

===

但是,这会改变X轴:

Graph After Changing Y Axis - It Unintentionally Changes X Axis

===

稍微分开的问题:我需要Y轴标签和刻度标记为整数。

关于这两个问题的想法?任何帮助表示赞赏!!

最佳,

1 个答案:

答案 0 :(得分:0)

要解决第二个问题(格式化轴标签),here是简单的格式化说明。

至于第一个问题,如果你只是swapping the two Y axes,那么缩放0到0.005在右边,缩放0到3.5在左边,你也会在轴选项中找到它