我目前正在尝试在工作表上创建一个标记为“ Unit2SelectedData”的嵌入式图形。发生的问题是当我从范围创建图形时,它不会跳过具有值#N / A / Blank的单元格,因此图例/值已关闭。
我尝试将单元格值更改为#N / A,当我手动创建图形时,它可以工作并跳过#N / A值。但是,当使用VBA自动化时,则不能。
到目前为止,我尝试了.DisplayBlanksAs = xlNotPlotted和Cells.Replace“#N / A”,vbNullString,它似乎并没有解决问题。我还尝试将值从#N / A或= NA()更改为完全空白。帮助将不胜感激!
我意识到主要的问题是我设置范围以获取图形数据的方式。我目前正在使用.SpecialCells(xlCellTypeConstants)
只从我设置为10:500的较大范围中获取恒定值。
如果删除了它并创建了图形,则跳过空白单元格,该图形看起来像下面所需示例中的序列值。但是,如果不是所有这些行都填充有数据,则该图会有大量的空列/系列,这会使图看起来很糟。
例如,如果我创建的图只填充了5行数据,那么图上将有495个额外的空白点。 (您可以在下面的代码中的“图表所选数据”块中看到。)
如何更改代码以解决此问题? /创建一个范围来选择数据的最后一行?
Sub GraphUnit2()
'Variables Declaration
'Range
Dim dataRange As Range
'Range 1
Dim dataRange1 As Range
'Range 2
Dim dataRange2 As Range
'Range 3
Dim dataRange3 As Range
'Range 4
Dim dataRange4 As Range
'Range 5
Dim dataRange5 As Range
'Range 6
Dim dataRange6 As Range
'Range 7
Dim dataRange7 As Range
'Range 8 (Dates + Time)
Dim dataRange8 As Range
'Chart Selected Data
'Range
Set dataRange = Sheets("Unit2SelectedData").Range("J10:J500").SpecialCells(xlCellTypeConstants)
'Range 1
Set dataRange1 = Sheets("Unit2SelectedData").Range("C10:C500").SpecialCells(xlCellTypeConstants)
'Range 2
Set dataRange2 = Sheets("Unit2SelectedData").Range("D10:D500").SpecialCells(xlCellTypeConstants)
'Range 3
Set dataRange3 = Sheets("Unit2SelectedData").Range("E10:E500").SpecialCells(xlCellTypeConstants)
'Range 4
Set dataRange4 = Sheets("Unit2SelectedData").Range("F10:F500").SpecialCells(xlCellTypeConstants)
'Range 5
Set dataRange5 = Sheets("Unit2SelectedData").Range("G10:G500").SpecialCells(xlCellTypeConstants)
'Range 6
Set dataRange6 = Sheets("Unit2SelectedData").Range("H10:H500").SpecialCells(xlCellTypeConstants)
'Range 7
Set dataRange7 = Sheets("Unit2SelectedData").Range("I10:I500").SpecialCells(xlCellTypeConstants)
'Range 8
Set dataRange8 = Sheets("Unit2SelectedData").Range("A10:B500").SpecialCells(xlCellTypeConstants)
'Chart Location/ Coordinates
Sheets("Unit2SelectedData").ChartObjects.Add Left:=900, Top:=50, Width:=800, Height:=400
Sheets("Unit2SelectedData").ChartObjects(1).Activate
With ActiveChart 'Set chart properties
.ChartType = xlLineMarkers
.ApplyLayout Layout:=5
.SeriesCollection.NewSeries
.HasLegend = True
.Legend.Position = xlRight
.Axes(xlCategory).MinorTickMark = xlOutside
.Axes(xlValue).MinorTickMark = xlOutside
.Axes(xlValue).MaximumScale = Application.WorksheetFunction.RoundUp(Application.WorksheetFunction.Max(dataRange), -1)
.Axes(xlCategory, xlPrimary).HasTitle = True
'X axis label
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Date/Time"
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Place"
.SeriesCollection(1).Name = "PlaceHolder"
.SeriesCollection(1).Values = dataRange
.SeriesCollection(1).XValues = dataRange8
.ChartTitle.Text = "Place Holder"
End With
'Data Series for Range 1
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange1
.Name = Sheets("Unit2SelectedData").Range("C3")
End With
'Data Series for Range 2
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange2
.Name = Sheets("Unit2SelectedData").Range("D3")
End With
'Data Series for Range 3
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange3
.Name = Sheets("Unit2SelectedData").Range("E3")
End With
'Data Series for Range 4
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange4
.Name = Sheets("Unit2SelectedData").Range("F3")
End With
'Data Series for Range 5
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange5
.Name = Sheets("Unit2SelectedData").Range("G3")
End With
'Data Series for Range 6
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange6
.Name = Sheets("Unit2SelectedData").Range("H3")
End With
'Data Series for Range 7
With ActiveChart.SeriesCollection.NewSeries
.Values = dataRange7
.Name = Sheets("Unit2SelectedData").Range("I3")
End With
End Sub
数据示例:
Date Value 1 Value 2 Value 3 Value 4 Value 5.... etc
11/07/2019 1 2 3 4 5
12/07/2019 #N/A #N/A 4 5 #N/A
13/07/2019 3 4 #N/A 6 7
14/07/2019 4 5 6 7 8
当我打印图形时,值为1系列。会是
July 11 1
July 12 3
July 13 4
July 14 0/blank
我理想的位置是
July 11 1
July 12 Blank/0/Skip on the graph
July 13 3
July 14 4
如您所见,当我将范围从J10:J40设置为10:40时,会有大量的空格。目前,所有这些单元格都未填充数据,但可能会填充数据。
这就是我想要的图形。
选项2图表
答案 0 :(得分:0)
首先,使用.SpecialCells(xlCellTypeConstants)
范围 STOP 。使用正常范围,例如Range("J10:J500")
。
解决方案:使用图表属性对缺失值进行插值或连接线。为此,请为每个图表添加以下行,替换ActiveChart
或图表变量或名称:
ActiveChart.DisplayBlanksAs = xlInterpolated