在所有 y 轴刻度上显示相同数字的图表

Chart displaying the same number on all y axis ticks

我是 VBA 的新手,所以我提供的代码可能远未优化。 不过,我很乐意听到可以改进的地方!

我有一个 sheet,其中包含两列数据、一个用于条目的单元格、一个散点图和三个按钮。 该图表应该简单地绘制两列并将输入单元格的值添加为垂直线。 一个按钮应该刷新图表(例如调整轴)。 另一个应该通过将存储在另一个隐藏 sheet 中的一些数据插入到两列以及输入单元格中,然后刷新图表来显示示例。 最后一个按钮应该只是重置 sheet,这样数据列和输入单元格都是空的,图表应该被隐藏。

问题: 但是刷新按钮不起作用。 它似乎根据新输入的数据调整图表的视野大小。但 y 轴上显示的数字始终为 0.05。 我提供了一张图片来向您展示我的意思:

当我按下示例按钮时,y 轴显示正确的值。我尝试先将列中的数据复制到不同的 sheet,然后刷新图表,然后将数据粘贴回列中,但没有成功。

我注意到,当图表仅显示 0.05 时,我单击图表 "select data" 并简单地 select 第二个系列,y 值更改为应有的值,我可以关闭window 它会保持原样,直到我再次按下刷新按钮。

也许我的代码有我不知道的缺陷?也许我需要,而不是刷新图表,每次删除旧图表并创建一个新图表?但是,我已经尝试过,结果相同。也许代码必须按不同的顺序编写?

示例按钮:

Private Sub ExampleBtn_Click()

Call RefreshBtn_Click

Range("C5:D27").ClearContents 'data columns

Range("C5:D14").Value = Sheets("ExampleSheet").Range("A1:B10").Value
Range("O3").Value = 3.5 'entry cell

End Sub

重置按钮:

Private Sub ResetBtn_Click()

ActiveSheet.ChartObjects(1).Visible = False

Range("C5:D27").ClearContents 'data columns
Range("O3").ClearContents 'entry cell
Range("C5").Select

End Sub

刷新按钮:

Private Sub RefreshBtn_Click()

ActiveSheet.Unprotect
Application.ScreenUpdating = False


ActiveSheet.ChartObjects(1).Visible = True

'range of Nachdruckzeit
Set zeit = Range("C5:C27")

'range of Masse
Set masse = Range("D5:D27")

yMax = Application.WorksheetFunction.max(Range("D5:D27")) + 0.02
yMin = Application.WorksheetFunction.min(Range("D5:D27")) - 0.02

'refresh the chart
With ActiveSheet.ChartObjects(1)
    .Width = Range("F5:P30").Width
    .Height = Range("F5:P30").Height

    If .Chart.SeriesCollection.Count = 1 Then
        .Chart.SeriesCollection(1).Delete
    ElseIf .Chart.SeriesCollection.Count = 2 Then
        .Chart.SeriesCollection(2).Delete
        .Chart.SeriesCollection(1).Delete
    End If

    With .Chart.SeriesCollection.NewSeries
        .XValues = zeit
        .Values = masse
        .Name = "Siegelpunktermittlung"
        .HasDataLabels = False
    End With

    t = Range("O3").Value 'entry cell
    With .Chart.SeriesCollection.NewSeries
        .Name = "gewaehlt"
        .XValues = Array(t, t)
        .Values = Array(0, yMax)
        .MarkerStyle = xlMarkerStyleNone
        .Border.Color = vbWhite
    End With
End With

'Cosmetics
With ActiveSheet.ChartObjects(1).Chart
  .HasLegend = False
  .ChartArea.Format.Fill.Visible = msoFalse
  .PlotArea.Format.Fill.Visible = msoFalse
  .SeriesCollection(1).MarkerStyle = xlMarkerStyleCircle
  .SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(251, 243, 223)
  .SeriesCollection(1).Format.Line.ForeColor.RGB = RGB(255, 140, 0)
  .SeriesCollection(1).MarkerForegroundColorIndex = -4142

  'x-axis
  With .Axes(xlCategory)
    .HasTitle = True
    .TickLabels.Font.Color = vbWhite
    .AxisTitle.Font.Size = 12
    .AxisTitle.Font.Color = vbWhite
    .AxisTitle.Caption = "Nachdruckzeit [s]"
  End With

  'y-axis
  With .Axes(xlValue)
    .HasTitle = True
    .MinimumScale = yMin
    .MaximumScale = yMax
    .TickLabels.Font.Color = vbWhite
    .AxisTitle.Font.Size = 12
    .AxisTitle.Font.Color = vbWhite
    .AxisTitle.Caption = "Masse [g]"
  End With
End With


Application.ScreenUpdating = True
ActiveSheet.Protect

End Sub


我找到了问题的解决方案:

问题是图表的数据单元格采用数字格式,无论出于何种原因,这都弄乱了图表。将格式更改为标准格式解决了问题。

如果有人知道为什么在更改单元格格式时会出现此问题,请告诉我。