![](/img/trans.png)
[英]excel VBA : how to skip blank cells between 2 cells that contain values?
[英]How to create a dynamic range & skip blank cells in excel vba
我目前正在嘗試在工作表上創建一個標記為“ 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圖表
首先,使用.SpecialCells(xlCellTypeConstants)
范圍停止 。 使用正常范圍,例如Range("J10:J500")
。
解決方案 :使用圖表屬性在缺失值上插值或連接線。 為此,請為每個圖表添加以下行,以替換ActiveChart
或圖表變量或名稱:
ActiveChart.DisplayBlanksAs = xlInterpolated
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.