Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Excel VBA中使系列跳过负值?_Vba_Excel_Linegraph - Fatal编程技术网

在Excel VBA中使系列跳过负值?

在Excel VBA中使系列跳过负值?,vba,excel,linegraph,Vba,Excel,Linegraph,到目前为止,这段代码创建了3个图表。最后一张图表有两个系列。第二个系列从一个负数开始,因此我在下面代码的末尾添加了cht.AxesxlValue.MinimumScale=0,以使y轴无论如何从零开始。唯一的问题是它让我的图表看起来很混乱。有什么我可以添加到现有的代码,使第二个系列跳过输入负数,并从第一个0或正数开始 Sub UpdateCharts() Dim cObj As ChartObject Dim cht As Chart Dim shtName As St

到目前为止,这段代码创建了3个图表。最后一张图表有两个系列。第二个系列从一个负数开始,因此我在下面代码的末尾添加了cht.AxesxlValue.MinimumScale=0,以使y轴无论如何从零开始。唯一的问题是它让我的图表看起来很混乱。有什么我可以添加到现有的代码,使第二个系列跳过输入负数,并从第一个0或正数开始

 Sub UpdateCharts()
    Dim cObj As ChartObject
    Dim cht As Chart
    Dim shtName As String
    Dim chtName As String
    Dim xValRange As Range
    Dim lastRow As Long

    With ActiveSheet
        lastRow = .Range("B" & .Rows.Count).End(xlUp).Row
        Set xValRange = .Range("B2:B" & lastRow)
        shtName = .Name & " "
    End With


    '## This sets values for Series 1 in each chart ##'
    For Each cObj In ActiveSheet.ChartObjects
        Set cht = cObj.Chart
        chtName = shtName & cht.Name

        If cht.SeriesCollection.Count = 0 Then
        '## Add a dummy series which will be replaced in the code below ##'
            With cht.SeriesCollection.NewSeries
                .Values = "{1,2,3}"
                .XValues = xValRange
            End With

        End If

        '## Assuming only one series per chart, we just reset the Values & XValues per chart ##'
        With cht.SeriesCollection(1)
        '## Assign the category/XValues ##'
            .Border.Color = RGB(0, 0, 255)
            .XValues = xValRange

        '## Here, we set the range to use for Values, based on the chart name: ##'
            Select Case Replace(chtName, shtName, vbNullString)
                 Case "RPM"
                      .Values = xValRange.Offset(0, 3) '## Column E is 3 offset from the xValRange in column B
                      .Name = "RPM"
                 Case "Pressure/psi"
                      .Values = xValRange.Offset(0, 5) '## Column G is 5 offset from the xValRange in column B
                      .Name = "Pressure/psi"
                 Case "Burn Off"
                    .Values = xValRange.Offset(0, 6)   '## Column H is 6 offset from the xValRange in column B
                    .Name = "Demand burn off"
                    '## Make sure this chart has 2 series, if not, add a dummy series ##'
                    If cht.SeriesCollection.Count < 2 Then
                        With cht.SeriesCollection.NewSeries
                            .XValues = "{1,2,3}"

                        End With
                    End If
                    '## add the data for second series: ##'
                    cht.SeriesCollection(2).XValues = xValRange
                    cht.SeriesCollection(2).Values = xValRange.Offset(0, 8)  '## Column J is 8 offset from the xValRange in column B
                    cht.SeriesCollection(2).Name = "Step Burn Off"
                    cht.SeriesCollection(2).Border.Color = RGB(255, 0, 0)

                 Case "Add as many of these Cases as you need"

            End Select

        End With
        cht.Axes(xlValue).MinimumScale = 0

    Next
    End Sub
图为:

如果我能很好地理解你的问题,一个非常简单的方法就是为带有负值的系列选择条或标记

在图形->左键单击->设置系列格式中选择系列,在第二个选项中选择“显示标记”,然后在第四个选项中的“线条颜色”中将线条设置为“不可见”

下面是一个例子:

在Vba中,要使用的属性类似于sthg:

ChartObjects("Graphique 1").MarkerStyle = -4109
With Selection code
.MarkerStyle = 3
.MarkerSize = 10
End With

你说它看起来杂乱无章是什么意思?我觉得你贴的很好。你能说得更具体些吗?哦,等等,我想我明白了。你是说你根本不想在步骤消耗为负的第一个条目中显示需求消耗序列,因此整个图表将在X轴上的15:11:37开始?没错,因此如果跳过负输入,X轴应该在15:11:37开始。它只是按现在的方式破坏了这条线,因为我将Y轴设置为从零开始。我认为最好找到第一个最接近0的值,并将其用作最小Y值。