简体   繁体   English

如何在Excel VBA中创建动态范围并跳过空白单元格

[英]How to create a dynamic range & skip blank cells in excel vba

I am currently trying to create an embedded graph on my worksheet labelled "Unit2SelectedData." 我目前正在尝试在工作表上创建一个标记为“ Unit2SelectedData”的嵌入式图形。 The problem that is occurring is when I create the graph from the ranges, it doesn't skip the cells with the value #N/A/Blank so the legend/values are off. 发生的问题是当我从范围创建图形时,它不会跳过具有值#N / A / Blank的单元格,因此图例/值已关闭。

I tried changing the cell values to #N/A and when I manually create the graph it works and skips the #N/A values. 我尝试将单元格值更改为#N / A,当我手动创建图形时,它可以工作并跳过#N / A值。 But when automating it with VBA it does not. 但是,当使用VBA自动化时,则不能。

So far I tried .DisplayBlanksAs = xlNotPlotted and Cells.Replace "#N/A", vbNullString and it did not seem to do the trick. 到目前为止,我尝试了.DisplayBlanksAs = xlNotPlotted和Cells.Replace“#N / A”,vbNullString,它似乎并没有解决问题。 I also tried changing the values from #N/A or =NA() to completely blank. 我还尝试将值从#N / A或= NA()更改为完全空白。 Help would be appreciated! 帮助将不胜感激!

I realized the main problem is the way I set my ranges to get data for the graph. 我意识到主要的问题是设置范围以获取图表数据的方式。 I am currently using .SpecialCells(xlCellTypeConstants) to only get constant values from a large range which i have set to cells 10:500. 我目前正在使用.SpecialCells(xlCellTypeConstants)仅从我设置为单元格10:500的较大范围中获取常量值。 If I removed this and create the graph I skip the blank cells and the graph looks like the series values in the desired example below. 如果删除了它并创建了图形,则跳过空白单元格,该图形看起来像下面所需示例中的序列值。 But, if not all those rows are populated with data the graph has a ton of empty columns/series and it makes the graph look bad. 但是,如果不是所有这些行都填充有数据,则该图会有大量的空列/系列,这会使图看起来很糟。 For example if I create the graph with only 5 rows populated with data, then there would be 495 extra blank points on the graph. 例如,如果我创建的图只填充了5行数据,那么图上将有495个额外的空白点。 (You can see in the code below in the 'Chart Selected Data' block.) How do I alter my code to fix this? (您可以在下面的“图表所选数据”块中的代码中看到。)如何更改代码以解决此问题? / Create a range that selects the last row with data? /创建一个范围来选择数据的最后一行?

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

Example of the Data: 数据示例:

    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

When I print the graph the value 1 series. 当我打印图形时,值为1系列。 It would be 这将是

    July 11 1 
    July 12 3 
    July 13 4 
    July 14 0/blank 

Where ideally I want it to be 理想情况下我希望它在哪里

    July 11 1 
    July 12 Blank/0/Skip on the graph  
    July 13 3 
    July 14 4 

As you can see theres a ton of blank spaces when I set the range from J10:J40 and other ranges to 10:40. 如您所见,当我将范围从J10:J40设置为10:40时,会有大量的空格。 For now, all these cells aren't filled with data, but they potentially will be. 目前,所有这些单元格都未填充数据,但可能会填充数据。 链接到图

This is what I want the graph to look like. 这就是我想要的图形。 链接到所需图形

Option 2 graph 选项2图表 链接到选项2图

First, STOP using .SpecialCells(xlCellTypeConstants) ranges. 首先,使用.SpecialCells(xlCellTypeConstants)范围停止 Use the normal ranges eg Range("J10:J500") . 使用正常范围,例如Range("J10:J500")

Solution : Use chart properties to interpolate or connect lines over missing values. 解决方案 :使用图表属性在缺失值上插值或连接线。 To do this, add the following line for each chart, replacing ActiveChart or chart variable or name: 为此,请为每个图表添加以下行,以替换ActiveChart或图表变量或名称:

ActiveChart.DisplayBlanksAs = xlInterpolated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM