excel - VBA从数组制作图表 - 缺少值的问题
问题描述
我正在制作带有标记的折线图。数据正在代码中创建到数组中,并将该数组放入图表中。
问题是数组中的缺失值表示为 EMPTY。
在绘制确实存在的两个点时,它们与线相连。如果单元格为空,则选择该选项以绘制间隙。
Serie 公式将 EMPTY 显示为 #N/A。
XValues=={"11/28/2016","12/5/2016","12/12/2016","12/19/2016","12/26/2016","1/2/2017 ","1/9/2017","1/16/2017","1/23/2017","1/30/2017","2/6/2017","2/13/2017", “2017 年 2 月 20 日”、“2017 年 2 月 27 日”}
Values ={125.15,93.875,#N/A,#N/A,#N/A,#N/A,42,125,48.5714285714285,137,127.285714285714,81.6428571428571,89.9375,69.5,65.6428571428571,75.5,47.1666666666666}
尝试用 0、“”、NaN 替换,没有任何效果。我想中断绘图线。
我有现有的价值,然后是一系列缺失值,然后是一些价值。我注意到,如果 serie 以缺失值开头,那么它的绘制效果很好。否则不工作。
For i = LBound(p_data, 1) + 1 To UBound(p_data, 1)
sSerieName = p_data(i, 0)
If sSerieName <> "" Then
Dim serie() As Variant
Dim w As Long
w = 0
For j = LBound(labels) To UBound(labels)
ReDim Preserve serie(j): serie(j) = p_data(i, j + 1)
Next j
If Not Len(Join(serie, "")) = 0 Then
On Error Resume Next
With p_chart.Chart.SeriesCollection.NewSeries
.XValues = labels
.Values = serie
.Name = sSerieName
End With
On Error GoTo 0
End If
End If
Next i
解决方案
我认为在通过数组设置值等时这是不可能的。它似乎解决Empty
为 N/A。我尝试了以下解决方案,这将涉及您记录这些空的发生位置。因此,例如,在这种情况下,我在第 3 点用之前的值替换了我的空值,并使用了以下
想要的值是 10,20,EMPTY,40
With s.NewSeries
.XValues = Array("a", "b", "c", "d")
.Values = Array(10, 20, 20, 40)
.Points(3).Format.Line.Visible = msoFalse
End With
我尝试的完整解决方案如下
Sub x()
Dim s As SeriesCollection
Dim lc As Long
Dim aTest() As Variant
Dim serie As String
aTest = Array(15, 20, Empty, 40)
Set s = ActiveChart.SeriesCollection
For lc = 0 To UBound(aTest)
If aTest(lc) = "" Then ' <--- record these lc values to hide points
If lc = 0 Then
aTest(lc) = 0
Else
aTest(lc) = aTest(lc - 1)
End If
Else
End If
Next lc
With s.NewSeries
.XValues = Array("a", "b", "c", "d")
.Values = aTest
.Points(3).Format.Line.Visible = msoFalse
End With
End Sub
推荐阅读
- python - 通过python加入服务器时,是否有绕过discord的可疑活动屏幕?
- python-3.x - ImportError:Debian 操作系统中没有名为 numpy 的模块
- ios - 在“优化 Mac 界面”模式下使用 Catalyst 构建应用程序时如何设置按钮标题颜色
- javascript - 如何解密加密的 JavaScript 代码?
- python - 包目录火币不存在
- javascript - 返回并存储来自异步函数的承诺结果
- mysql - 如何在 SQL 4.9.5 中替换 WITH 子句。我在版本 8 中使用它,但我的服务器不支持它
- python - 如何让我的函数仅在端点的特定变量发生变化时调用?(Python)
- pine-script - Pine 编辑器获取特定栏的真实范围
- html - 如何让我的按钮在 jquery 中多次工作?