EXCEL 2016 VBA:交换Y轴和Y轴

我试图编程生成一个直方图与正常曲线叠加在它上面。 我从各种帮助页面和post收集的一般策略是创build一个xlXYScatterSmoothNoMarkers,为正常曲线的x和y值添加数据序列,然后将我的直方图数据(中断和频率)添加为另一个数据序列,以及然后将直方图数据序列更改为xlColumnClustered,并进行一些最终的格式化。 但是,当我接近尾声时,正常曲线的密度值位于主Y轴上,频率值位于次Y轴上。 我正在寻找一种方法来切换它们。 这是迄今为止的图表的图片:

截至目前为止的截图

这是我现在的代码:

===

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 

===

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

===

  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轴:

Y轴改变后的graphics – 无意中改变了X轴

===

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

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

最好,

要解决你的第二个问题(格式化轴标签), 这里是简单的格式化指令。

至于第一个问题,如果你真的只是交换两个Y轴 ,所以0到0.005的比例在右边,0到3.5的比例在左边,你也可以在轴选项中find。